|
Here we render all the pages of the doc using 10 threads at a
time. We alternate rendering format between jpg and tiff. We also
alternate resolution between 150 and 300 dpi. Note how the
RenderingOperation is created in the constructor of
TheRenderingWorker. This is because at this point a copy of the
rendering options is made. Had we created the RenderingOperation in
DoWork, we would have picked up only the last
doc.Rendering.DotsPerInch, because the threads are started in the
following loop. Also note how we dispose the operation in DoWork,
to release resources stored on the native side (the copy of the
rendering options basically).
[C#]class
RenderingWorker
{
private string mPath;
private RenderOperation mOp;
public RenderingWorker(Doc inDoc, string
inPath)
{
mPath = inPath;
mOp = new
RenderOperation(inDoc);
}
public void DoWork()
{
mOp.Save(mPath);
mOp.Dispose();
}
}
Doc doc = new Doc();
doc.Read(Server.MapPath("ABCpdf.pdf"));
string[] theExts = { ".jpg", ".tif" };
int[] theDpis = { 150, 300 };
const int NUM_THREADS = 10;
Thread[] threadList = new Thread[NUM_THREADS];
while (doc.PageNumber < doc.PageCount)
{
for (int i = 0; i < threadList.Length;
++i)
{
if (threadList[i]
!= null)
{
threadList[i]
= null;
}
}
for (int i = 0; i < threadList.Length;
++i)
{
doc.Rendering.DotsPerInch
= (i % 2) == 0 ? theDpis[0] : theDpis[1];
string ext = (i %
2) == 0 ? theExts[0] : theExts[1];
string path =
Server.MapPath("ABCpdf" + doc.PageNumber.ToString() + ext);
threadList[i] = new
Thread(new RenderingWorker(doc, path).DoWork);
if (doc.PageNumber
== doc.PageCount)
{
break;
}
else
{
doc.PageNumber++;
}
}
for (int i = 0; i < threadList.Length;
++i)
{
if (threadList[i]
!= null)
{
threadList[i].Start();
}
}
for (int i = 0; i < threadList.Length;
++i)
{
if (threadList[i]
!= null)
{
threadList[i].Join();
}
}
}
doc.Clear();
[Visual Basic]Class
RenderingWorker
Private mPath As String
Private mOp As RenderOperation
Public Sub New(ByVal inDoc As Doc, ByVal
inPath As String)
mPath = inPath
mOp = New
RenderOperation(inDoc)
End Sub
Public Sub DoWork()
mOp.Save(mPath)
mOp.Dispose()
End Sub
End Class
Dim doc As Doc = new Doc()
doc.Read(Server.MapPath("ABCpdf.pdf"))
Dim theExts As String() = {".jpg", ".tif"}
Dim theDpis As Integer() = {150, 300}
Const NUM_THREADS As Integer = 10
Dim threadList(NUM_THREADS - 1) As Thread
While doc.PageNumber < doc.PageCount
For i As Integer = 0 To threadList.Length -
1
If threadList(i)
IsNot Nothing Then
threadList(i)
= Nothing
End If
Next
For i As Integer = 0 To threadList.Length -
1
doc.Rendering.DotsPerInch
= If((i Mod 2) = 0, theDpis(0), theDpis(1))
Dim ext As String =
If((i Mod 2) = 0, theExts(0), theExts(1))
Dim path As String
= Server.MapPath(("ABCpdf" & doc.PageNumber.ToString()) +
ext)
threadList(i) = New
Thread(AddressOf New RenderingWorker(doc, path).DoWork)
If doc.PageNumber =
doc.PageCount Then
Exit
For
Else
doc.PageNumber
+= 1
End If
Next
For i As Integer = 0 To threadList.Length -
1
If threadList(i)
IsNot Nothing Then
threadList(i).Start()
End If
Next
For i As Integer = 0 To threadList.Length -
1
If threadList(i)
IsNot Nothing Then
threadList(i).Join()
End If
Next
End While
doc.Clear()
|
|
|