Add Page Number Stamp in a PDF File in VB - Aspose.PDF for .NET - Out of memory Error

Hi Team,



I am using vb.net code which you have provided on below link and trying to give page number to PDF which has 15000 pages.

But this gives me out of memory error.

please help me to overcome this.

I am using latest aspose.pdf dll.



Stamping with Aspose.PDF using C#|Aspose.PDF for .NET



how can I save the file in incremental mode like after each 1000 page I want to save.

Please reply me as this is very urgent issue for me.

rajsingh85007:
Hi Team,

I am using vb.net code which you have provided on below link and trying to give page number to PDF which has 15000 pages.
But this gives me out of memory error.
please help me to overcome this.
I am using latest aspose.pdf dll.

Stamping with Aspose.PDF using C#|Aspose.PDF for .NET

Hi Rajsingh,

Thanks for contacting support. Can you please share the source PDF file so that we can test the scenario at our end.
rajsingh85007:
how can I save the file in incremental mode like after each 1000 page I want to save.
Aspose.Pdf for .NET manipulates the PDF file and save the resultant document at once. However as a workaround, you may consider creating a temporary PDF file of 1000 pages, perform the manipulation (add text stamp) and save the output. You can repeat the same process for required number of times and then concatenate the resultant files. For further details, please visit


We are sorry for this inconvenience.

I will send pdf soon but how can I add page stamp and then concatenate? support I have two pdf one is of 1000 page and other is of 800 page. when I merge these two I want to show page 1 of 1800, page 2 of 1800 and so on. how to get total number of pages from two separate pdf?

File attached.

Hi Rajsingh,


Well in case you need to concatenate PDF files and need to add Stamp object according to concatenated file, then you need to first load the individual PDF files, get page count information as it will give the total number of pages that will be part of resultant/merged file. Then you can stamp the page number information and then merge the PDF files. In order to get the page count, you may try using following code line.

[C#]

//
load source PDF file
<o:p></o:p>

Document doc = new Document("c:/pdftest/1.pdf");

// get total page count information

<span style=“font-size:9.5pt;line-height:115%;font-family:Consolas;mso-fareast-font-family:
“Malgun Gothic”;mso-fareast-theme-font:minor-fareast;color:#2B91AF;background:
white;mso-highlight:white;mso-ansi-language:EN-US;mso-fareast-language:KO;
mso-bidi-language:AR-SA”>Console.WriteLine(<span style=“font-size:9.5pt;line-height:115%;font-family:Consolas;mso-fareast-font-family:
“Malgun Gothic”;mso-fareast-theme-font:minor-fareast;color:#A31515;background:
white;mso-highlight:white;mso-ansi-language:EN-US;mso-fareast-language:KO;
mso-bidi-language:AR-SA”>"Total pages in PDF = " +doc.Pages.Count);

please send me the vb.net code for the full procedure for my requirement.

Can you please respond to my query ASAP?

Hi Rajsingh,


Please try using following code snippet to accomplish your requirement.

[VB.NET]


instantiate Document Object
<o:p></o:p>

Dim doc As Document = New Document("c:/pdftest/1.pdf")

Dim doc2 As Document = New Document("c:/pdftest/ChartsandGraphs.pdf")

' concatenate the page of PDF documents

doc.Pages.Add(doc2.Pages)

'create page number stamp

Dim pageNumberStamp As New PageNumberStamp()

'whether the stamp is background

pageNumberStamp.Background = False

pageNumberStamp.Format = "Page # of " & doc.Pages.Count

pageNumberStamp.BottomMargin = 10

pageNumberStamp.HorizontalAlignment = HorizontalAlignment.Center

pageNumberStamp.StartingNumber = 1

'set text properties

pageNumberStamp.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Arial")

pageNumberStamp.TextState.FontSize = 14.0F

pageNumberStamp.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold

pageNumberStamp.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Italic

pageNumberStamp.TextState.ForegroundColor = Color.Aqua

Dim page_counter As Integer = 1

For Each page As Page In doc.Pages

'add stamp to particular page

doc.Pages(page_counter).AddStamp(pageNumberStamp)

page_counter += 1

Next

doc.Save("c:/pdftest/StampAdded.pdf")

Thank for this but when we do doc.Pages.Add(doc2.Pages) it will again have issue of out of memory.

Hi Rajsingh,


In this scenario, I will suggest you to please split the large PDF into smaller size PDF files (i.e. 100 pages PDF) and then use individual small files to generate the single resultant file. In order to create smaller size documents, you may consider extracting the pages from large file and saving them separately. For further details, please visit Concatenate PDF Files.

In case you encounter any issue while concatenating the PDF files, please share the resource files so that we can test the scenario at our end.

I have created small files and now i want to give stamp like pdf 1 has got 1000 page and pdf 2 got 800.i have also got total number of pages count as 1800 in variable.now when i am giving stamp to individual file first file is getting 1 of 1800, 2 of 1800 and so on.but 2nd file is also getting 1 of 1800 , 2 of 1800 and so on.but i want to give second file as 1001 of 1800,1002 of 1800 and so on.meaning i want to pass value of # for the below syntax whichi should increment.

pageNumberStamp.Format = "Page # of " & i

rajsingh85007:

I have created small files and now i want to give stamp like pdf 1 has got 1000 page and pdf 2 got 800.i have also got total number of pages count as 1800 in variable.now when i am giving stamp to individual file first file is getting 1 of 1800, 2 of 1800 and so on.but 2nd file is also getting 1 of 1800 , 2 of 1800 and so on.but i want to give second file as 1001 of 1800,1002 of 1800 and so on.meaning i want to pass value of # for the below syntax whichi should increment.

pageNumberStamp.Format = "Page # of " & i

Hi Rajsingh,

In order to start the page count for second file (800 pages file) from 1001, use the starting number as PageCount of first PDF + 1 (i.e. new Document("1000PagePDF.pdf").pages.count + 1) and use this value as starting number for second PDF file.

With 6000 page file which is of size 31 mb i am getting out of memory error.i cannot further break this file. also i am unable to attach this file over here.please let me know how to send that file so that you can replicate this issue.

In my below code i get out of memory when i try to add stamp for page 630.

doc10 = New Document"\\ysiinvgs3Reports70rel\Demo\temp\OG\MINTRA_42430010_0.pdf")
For Each page As Page In doc10.Pages
pageNumberStamp.Format = "Page" & total_page_counter & " of " & i
doc10.Pages(page_counter).AddStamp(pageNumberStamp)
page_counter += 1
Next
doc10.Save("\\ysiinvgs3\Reports70rel\Demo\temp\OG\MINTRA_424300010_S.pdf")

please help me in solve this issue as its very urgent now.

Hi Rajsingh,


Please upload the file to some free FTP (i.e. GoogleDrive, SkyDrive, DropBox etc) and share the link with us. We will test the scenario and will share our findings.

Here is the file.
https://drive.google.com/file/d/0B0VMx4LFjvKoRHk0eDhEWWJac0U/edit?usp=sharing

please take it and let me know ASAP as this is super urgent.

rajsingh85007:
Here is the file. https://drive.google.com/file/d/0B0VMx4LFjvKoRHk0eDhEWWJac0U/edit?usp=sharing

please take it and let me know ASAP as this is super urgent.

Hi Rajsingh,

Thanks for sharing the resource file. However it looks like file is protected and I have sent a request to access this file. Please allow the privileges to download the file.

I have given access of file.
please investigate and let me know ASAP.

Hi Rajsingh,


Thanks for granting the permissions. I have tested the scenario using following code snippet with latest release of Aspose.Pdf for .NET 9.5.0 and I am unable to notice any problem when using VisualStudio 2010 project with target platform as .NET Framework 4.0, running over Windows 7 (x64) machine. My system configurations are Intel Core i5, 2.5 Ghz with 4 GB of RAM.

Can you please try using the latest release version and see if it resolves your problem. For your reference, I have also attached the resultant PDF generated over my end. Please access the file over this link.

[VB.NET]

’ instantiate Document Object<o:p></o:p>

Dim doc As Document = New Document("c:/pdftest/AAMINTRA_42430011_0.pdf")

'Dim doc2 As Document = New Document("c:/pdftest/ChartsandGraphs.pdf")

' concatenate the page of PDF documents

'doc.Pages.Add(doc2.Pages)

'create page number stamp

Dim pageNumberStamp As New PageNumberStamp()

'whether the stamp is background

pageNumberStamp.Background = False

pageNumberStamp.Format = "Page # of " & doc.Pages.Count

pageNumberStamp.BottomMargin = 10

pageNumberStamp.HorizontalAlignment = HorizontalAlignment.Center

pageNumberStamp.StartingNumber = 1

'set text properties

pageNumberStamp.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Arial")

pageNumberStamp.TextState.FontSize = 14.0F

pageNumberStamp.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold

pageNumberStamp.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Italic

pageNumberStamp.TextState.ForegroundColor = Aspose.Pdf.Color.Aqua

Dim page_counter As Integer = 1

For Each page As Page In doc.Pages

'add stamp to particular page

doc.Pages(page_counter).AddStamp(pageNumberStamp)

page_counter += 1

Next

doc.Save(“c:/pdftest/AAMINTRA_42430011_0_StampAdded.pdf”)

Can you try with framework 2 target platform as I am having that.

Hi Rajsingh,


I have again tested the scenario with .NET Framework 2.0 and I am unable to notice any issue. Can you please share some sample application so that we can again try replicating the issue at our end.