I'm trying to create a little utility that splits an existing PDF into even and odd pages. The program works for small files, but completely saturates memory for a moderate file.
The PDF file I'm testing has 1280 pages of text generated by a report writer. The total file size is 980KB. Odd pages have text on them and even pages are blank.
The technique I'm using is to read the source file odd pages and merge them to a document in memory. When all the selected pages have been merged, I write the document to a file. I repeat for the even pages.
When I run my program (VB.NET 2005 on Vista Ultimate on a machine with 2GB of RAM) and monitor the application, the memory usage quickly exceeds 1.5GB after processing 300-400 pages. I have some progress meters plus the system task manager that I can watch to see what is happening. The program succeeds in writing the file for the odd pages, but the program locks up the machine about 100 pages into the even number page merge. It appears to keep working, but it is deep into some kind of memory swapping so that it is effectively halted. It makes no further progress and I have to force it to quit.
Can anyone help me understand the memory usage? I can't quite picture how processing the pages of a 1200 page 1MB source file consumes 1.5GB of memory by the 300-400th page. I'm not sure if the memory is going to the DynamicPDF library, the .NET framework, or what.
Here is the core code:
Public Function SplitPDFFile(ByVal isInputFile As String, ByVal isSplitInto As String, ByVal isOutputFile As String, ByRef osOutputMsg As String, ByRef icProgress As clsProgress, ByVal isSettings As Settings) As Integer
Dim lPageNo As Long
Dim lPageCount As Long
Dim lSetNo As Long
Dim lSetCount As Long
Dim nDocItem As Integer
Dim bStart As Boolean
Dim nSplitInto As Integer
Dim cSplitDoc As PDFDoc
Dim sOutputName As String
Dim lCurrentPage As Long
nSplitInto = CType(isSplitInto, Integer)
lPageCount = ReadDocPages(isInputFile)
If lPageCount < isSettings.nLimitPages Then
icProgress.OutputPages = lPageCount
Else
icProgress.OutputPages = isSettings.nLimitPages
End If
lCurrentPage = 0
lSetCount = lPageCount \ nSplitInto
For nDocItem = 1 To nSplitInto
bStart = True
For lSetNo = 1 To lSetCount
lPageNo = (lSetNo - 1) * nSplitInto + nDocItem
lCurrentPage = lCurrentPage + 1
If bStart Then
cSplitDoc = New PDFDoc
cSplitDoc.CreateMergeDoc(isInputFile, lPageNo)
If lPageNo >= nSplitInto Then
bStart = False
End If
Else
cSplitDoc.AddPage(isInputFile, lPageNo)
End If
icProgress.CurrentPage = lCurrentPage
icProgress.Message = "Processing page " & icProgress.CurrentPage & " of " & icProgress.OutputPages & " pages"
Application.DoEvents()
If lPageNo >= isSettings.nLimitPages Then
Exit For
End If
Next lSetNo
sOutputName = isOutputFile & CType(nDocItem, String) & ".PDF"
icProgress.Message = "Writing file " & sOutputName
Application.DoEvents()
cSplitDoc.Document.Draw(sOutputName)
cSplitDoc = Nothing
Application.DoEvents()
Next nDocItem
icProgress.Message = "Done"
End Function
Public Class PDFDoc
Dim mDocPage As ceTe.DynamicPDF.Page
Dim mDocument As ceTe.DynamicPDF.Merger.MergeDocument
Dim mMargins As cRectBounds
Dim mnCurrentPage As Short
Public Property Document() As ceTe.DynamicPDF.Merger.MergeDocument
Get
Return mDocument
End Get
Set(ByVal value As ceTe.DynamicPDF.Merger.MergeDocument)
mDocument = value
End Set
End Property
Public Sub CreateMergeDoc(ByVal isInputFilePath As String, ByVal ilPageNo As Long, Optional ByVal idMarginLeft As Single = 18, Optional ByVal idMarginTop As Single = 18, Optional ByVal idMarginRight As Single = 0, Optional ByVal idMarginBottom As Single = 0)
Dim bTest As Boolean
bTest = ceTe.DynamicPDF.Document.AddLicense("my license goes here")
mDocument = New ceTe.DynamicPDF.Merger.MergeDocument
mDocument.Append(isInputFilePath, ilPageNo, 1)
mnCurrentPage = 0
mMargins = New cRectBounds
GoToDocPage(mnCurrentPage)
End Sub
Public Sub AddPage(ByVal isSourcePDF As String, ByVal ilPageNo As Long)
mDocument.Append(isSourcePDF, ilPageNo, 1)
End Sub
Public Function NextDocPage() As Boolean
mnCurrentPage = mnCurrentPage + 1
If mnCurrentPage > mDocument.Pages.Count Then
Return False
Exit Function
End If
mDocPage = mDocument.Pages(mnCurrentPage)
mDocPage.Dimensions.SetMargins(mMargins.Left, mMargins.Top, mMargins.Right, mMargins.Bottom)
Return True
End Function
Public Function GoToDocPage(ByVal inDocPage As Short) As Boolean
mnCurrentPage = inDocPage
If mnCurrentPage > mDocument.Pages.Count Then
Return False
Exit Function
End If
mDocPage = mDocument.Pages(mnCurrentPage)
mDocPage.Dimensions.SetMargins(mMargins.Left, mMargins.Top, mMargins.Right, mMargins.Bottom)
Return True
End Function
End Class
Thanks,
Rick
Memory usage problem
Re: Memory usage problem
Support · Jun 18, 2007Hello Rick,
We have been able to create PDF documents of around 300MB in size successfully. Please double check your code for any object is causing this error. Follwoing is the code we used to split the odd and even pages of a PDF document and saved them to two different PDF documents. This code worked fine for a PDF of 3MB size and of pages 1200 without any problem. Please try this code and see if you get the error.
MergeDocument pdf = new MergeDocument( @"C:\Temp\DocA.pdf" );
MergeDocument oddDoc = new MergeDocument();
MergeDocument evenDoc = new MergeDocument();
bool odd = true;
foreach( Page page in pdf.Pages )
{
if( odd )
{
oddDoc.Pages.Add( page );
odd = false;
}
else
{
evenDoc.Pages.Add( page );
odd = true;
}
}
oddDoc.Draw( @"C:\Temp\OddDocA.pdf" );
evenDoc.Draw( @"C:\Temp\EvenDocA.pdf" );
Thanks,
ceTe Software Support Team.
Re: Memory usage problem
Forum member · Jun 18, 2007Thanks! I implemented your code in VB.NET and it made an amazing difference.
1) It worked
2) It completed almost instantly
Since you know the internals of your functions and I don't, I'm guessing you chose an optimal method, and I chose some other method that required a huge amount of memory and more time. I could take a guess, but basically I just have to copy your example. It would be very helpful if you could write some application notes or a technical article about the correct method for using your libraries and things to avoid.
Thanks again!
Rick
Re: Memory usage problem
Forum member · Jun 18, 2007I see now that I'm getting an evaluation background on my documents with error code 4:0:d0. I wasn't having licensing problems before.
I've added licensing code to my application in various places with no effect. I went to the customer service area and regenerated my license code. It's the same as I have been using.
Any ideas?
Rick
Re: Memory usage problem
Forum member · Jun 18, 2007Oops, false alarm. My first function didn't have the licensing in the right place, and I kept looking at it instead of newer files.