C itextsharp read pdf

Sign in with

Technical Blog

Comments and Discussions

You must Sign in to use this message board.
First Prev Next
Michael Breeden 20-Jul-20 1:47

public static bool MergePDFs(List lststrInputFiles, string OutputFile, out int iPageCount, out string strError)
strError = string.Empty;

PdfWriter pdfWriter = new PdfWriter(OutputFile); PdfDocument pdfDocumentOut = new PdfDocument(pdfWriter); PdfReader pdfReader0 = new PdfReader(lststrInputFiles[0]); PdfDocument pdfDocument0 = new PdfDocument(pdfReader0); int iFirstPdfPageCount0 = pdfDocument0.GetNumberOfPages(); pdfDocument0.CopyPagesTo(1, iFirstPdfPageCount0, pdfDocumentOut); iPageCount = pdfDocumentOut.GetNumberOfPages(); for (int ii = 1; ii < lststrInputFiles.Count; ii++) < PdfReader pdfReader1 = new PdfReader(lststrInputFiles[ii]); PdfDocument pdfDocument1 = new PdfDocument(pdfReader1); int iFirstPdfPageCount1 = pdfDocument1.GetNumberOfPages(); iPageCount += iFirstPdfPageCount1; pdfDocument1.CopyPagesTo(1, iFirstPdfPageCount1, pdfDocumentOut); int iFirstPdfPageCount00 = pdfDocumentOut.GetNumberOfPages(); > pdfDocumentOut.Close(); return true; >
BjarkeCK 21-May-18 4:24
Useful! One minor thing though - your try catches literally does nothing
Sign in· View Thread
Nareshkumar26 13-Nov-17 20:30
Hello,
I am using pdfwriter to merge two or more pdfs in one file but, I have one issue is that,Internal links gets removed. e.g. I have inter linked files like suppose 1.pdf have 2 pages on 1st page there is link of 2nd page on some text. after using pdfwriter merge code the links are getting removed, they are no longer in use. Can any one please guide me?
Sign in· View Thread
Mokkujin 21-Jul-16 4:39 Michael Clinton 2-Oct-15 9:34
I am trying to split all the pages and have all pages in memory so that I can run a QR code reader on each page. Pages are not guaranteed to be in order.
Thanks
mjc
Sign in· View Thread
jwphrijk 25-Sep-15 21:30

Could you give me a hint how I can extract the pages with the same orientation as the Original, not just with the same orientaion as the first page.

Thanks in advance for your help

Member 10585626 30-Apr-15 5:11
Hi. I am using the approach described in this article to split a large PDF into a set of smaller PDFs, using a set of page ranges. This works fine, except that the combined size of the smaller PDFs is from 10 to 50 times the size of the original PDF! Is there an explanation for this? Is there some parameter I can set to reduce the size of the output files? I am using running my PDF from my Windows 7 desktop which has Adobe Acrobat XI. Thanks for any help you can provide.
Sign in· View Thread
Bananenbrot 24-Jul-14 21:22

your solution works really great. But is there also a way to keep the AcroFields with their values in the output file? The fields are in the pdf but they are somehow hidden. The value of a field is only visible when the field is focused.

nevil_119 18-Mar-14 18:26

You can follow this. Using this you will be able to split pdfs page by page or at interval of 5 pages. http://www.technonutty.com/2013/10/how-to-split-pdf-documents-using-c.html[^]

Sajitha N Rathnayake 1-Mar-14 15:43


string[] lstFiles=new string[3];
lstFiles[0]=@"C:/pdf/1.pdf";
lstFiles[1]=@"C:/pdf/2.pdf";
lstFiles[2]=@"C:/pdf/3.pdf";

PdfReader reader = null;
Document sourceDocument = null;
PdfCopy pdfCopyProvider = null;
PdfImportedPage importedPage;
string outputPdfPath=@"C:/pdf/new.pdf";


sourceDocument = new Document();
pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));

//Open the output file
sourceDocument.Open();

try
//Loop through the files list
for (int f = 0; f < lstFiles.Length-1; f++)
int pages =get_pageCcount(lstFiles[f]);

reader = new PdfReader(lstFiles[f]);
//Add pages of current file
for (int i = 1; i importedPage = pdfCopyProvider.GetImportedPage(reader, i);
pdfCopyProvider.AddPage(importedPage);
>

reader.Close();
>
//At the end save the output file
sourceDocument.Close();
>
catch (Exception ex)
throw ex;
>


private int get_pageCcount(string file)
using (StreamReader sr = new StreamReader(File.OpenRead(file)))
Regex regex = new Regex(@"/Type\s*/Page[^s]");
MatchCollection matches = regex.Matches(sr.ReadToEnd());

John Atten 1-Mar-14 15:51

How did it work for you? Off the cuff, looks like it should work a charm. I can't run it at the moment, but nicely done!

I had to come up with a similar implementation for a project at work.