I’m a java guy, but I’m curious about this method signature:

 

static bool RewritePdfFiles(string pdfFile, string outputFile)

 

if you are trying to merge PDFs, shouldn’t you be doing this:

 

static bool MergePdfs(// an array of PDFs, string mergedPdfFile);

 

or maybe even

 

static [bytes or a stream] MergePdfs(// an array of PDFs);

 

and let the caller deal with the output?

 

 

From: Nick Bannister [mailto:[email protected]] 
Sent: Wednesday, June 20, 2012 3:59 AM
To: [email protected]; [email protected]
Subject: Re: [iText-questions] PDF merge - filesize after

 

OK attached are my example PDFs.
 
My code is below:
 
public


static bool RewritePdfFiles(string pdfFile, string outputFile)

{



bool bOK = true;

iTextSharp.text.pdf.

PdfSmartCopy pdfWriter = null;

iTextSharp.text.pdf.

PdfReader pdfReader = null;



//no input file found



if (pdfFile == null || pdfFile == "")

{



throw new System.Exception("There is no input pdf files to merge!");

}



//string[] fileList = pdfFiles;



//init



int pageOffset = 0;



//ArrayList master = new ArrayList();

 



IList<Dictionary<string, object>> master = new List<Dictionary<string, 
object>>();

iTextSharp.text.

Document pdfDocument = null;



int totalProcessedPages = 0;



try

{



FileStream tmpFile = System.IO.File.Create(outputFile);



//foreach (KeyValuePair<string, string> kvp in pdfFiles)



//{



//Key - Filepath



//Value - Bookmark



int currentDocumentPages = 0;

pdfReader = 

new iTextSharp.text.pdf.PdfReader(pdfFile);

pdfReader.RemoveUnusedObjects();

pdfReader.ConsolidateNamedDestinations();



int pdfNumberOfPages = pdfReader.NumberOfPages;



//String boundsString = "";



IList<Dictionary<string, object>> bookmarks = new List<Dictionary<string, 
object>>();



// IList<Dictionary<String, Object>> bookmarks = new ArrayList<HashMap<String, 
Object>>();



int relativeOffset = pdfNumberOfPages;

currentDocumentPages += relativeOffset;

pageOffset += relativeOffset;



Console.WriteLine(pdfFile + ": " + currentDocumentPages + " pages to be 
added.");



if (pdfWriter == null)

{



// step 1: creation of a document-object

pdfDocument = 

new iTextSharp.text.Document(pdfReader.GetPageSizeWithRotation(1));



// step 2: we create a writer that listens to the document



//pdfWriter = new iTextSharp.text.pdf.PdfCopy(pdfDocument, tmpFile);

pdfWriter = 

new iTextSharp.text.pdf.PdfSmartCopy(pdfDocument, tmpFile);

pdfWriter.CompressionLevel = 

PdfStream.BEST_COMPRESSION;

pdfWriter.SetFullCompression();



// step 3: we open the document

iTextSharp.text.pdf.

PdfName version = new iTextSharp.text.pdf.PdfName("8");

pdfWriter.SetPdfVersion(version);

pdfDocument.AddCreator(

"Cad-Capture");

pdfDocument.Open();



//}

}



// step 4: we add content



if (pdfReader != null)

{



int numPages = pdfReader.NumberOfPages;



for (int count = 1; count <= numPages; count++)

{

pdfWriter.AddPage(pdfWriter.GetImportedPage(pdfReader, count));

}

}



else

{



throw new System.Exception("Reader is null");

}

pdfWriter.FreeReader(pdfReader);

pdfReader.Close();

totalProcessedPages += currentDocumentPages;



Console.WriteLine(currentDocumentPages + " pages processed correctly.");



//}



Console.WriteLine("Total processed pages: " + totalProcessedPages + ".");



if (pdfDocument != null)

{

pdfDocument.Close();

}



Console.WriteLine("File " + outputFile + " created.");

}



catch (Exception e)

{

bOK = 

false;



return bOK;



throw e;

}



finally

{

}



return bOK;

}


This is a transmission from Purdue Federal Credit Union (Purdue Federal) and is 
intended solely for its authorized recipient(s), and may contain information 
that is confidential and or legally privileged. If you are not an addressee, or 
the employee or agent responsible for delivering it to an addressee, you are 
hereby notified that any use, dissemination, distribution, publication or 
copying of the information contained in this email is strictly prohibited. If 
you have received this transmission in error, please notify us by telephoning 
(765)497-3328 or returning the email. You are then instructed to delete the 
information from your computer. Thank you for your cooperation.
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to