Well, I finally end up using PdfCopy instead of PdfWriter or PdfCopyFields.
Nothing to worry about page rotation so far.
Thank you Eric and Gunvant for your help and ideas.
Here is an excerpt of my code in case somebody needs to do a merge using
iTextSharp.
-------
PdfCopy copy = new PdfCopy(document, new
FileStream(string.Format("{0}\\{1}.pdf", SaveTo, BatchID), FileMode.Create));
document.Open();
// Holds the PDF data
PdfImportedPage page;
int currentPageNumber = 0;
int pageOfCurrentReaderPDF = 0;
// Iterate over PdfReaders
foreach (PdfReader pdfreader in readers)
{
while (pageOfCurrentReaderPDF < pdfreader.NumberOfPages)
{
// Increase page counters
currentPageNumber++;
pageOfCurrentReaderPDF++;
// Get page from reader
page = copy.GetImportedPage(pdfreader, pageOfCurrentReaderPDF);
// Add new page to the main document
copy.AddPage(page);
}
// Re-initialize current document page counter
pageOfCurrentReaderPDF = 0;
}
----------
Some portions of my code were also taken from:
http://java-x.blogspot.com/2006/11/merge-pdf-files-with-itext.html#comment-1940362251951690324
Thank you all!
From: gunvant sahu [mailto:[email protected]]
Sent: Friday, May 21, 2010 1:51 AM
To: Post all your questions about iText here
Subject: Re: [iText-questions] Document rotates while merging
Try this one
public class MyMergePDF2 {
/**
* @param args
* @throws IOException
* @throws DocumentException
*/
public static void main(String[] args) throws IOException {
//creating list of readers of pdf files
try {
PdfReader reader1 = new PdfReader("D:/15.pdf");
PdfReader reader2 = new PdfReader("D:/14.pdf");
PdfReader reader3 = new PdfReader("D:/16.pdf");
List pdfReaderList = new ArrayList(); //Adding readers to the files
pdfReaderList.add(reader1);
pdfReaderList.add(reader2);
pdfReaderList.add(reader3);
// Creating the PdfCopyFields
// This copy field object will take the reader object and copy the pages to its
object(copy)
// So just by adding a document we can create a PDF with your merged pdf
PdfCopyFields copy = new PdfCopyFields(new FileOutputStream(
"D:/memory/Output.pdf"));
copy.open();
if (null != pdfReaderList && !pdfReaderList.isEmpty()) {
Iterator iter = pdfReaderList.iterator();
while (iter.hasNext()) {
String pageNOs = "";
PdfReader pdfReader = (PdfReader) iter.next();
int noOfPages = pdfReader.getNumberOfPages();
if (noOfPages > 0) {
pageNOs = getNumderOfPages(noOfPages);
}
copy.addDocument(pdfReader, pageNOs);
}
}
copy.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Function to get page numbers in string with comma separated
* @param noOfPages
* @return
*/
private static String getNumderOfPages(int noOfPages) {
String pageNOs = "";
boolean flag = false;
for (int i = 0; i < noOfPages; i++) {
if (flag == true) {
Integer c = (Integer) i;
pageNOs = pageNOs.concat("," + c.toString());
}
if (flag == false) {
Integer c = (Integer) i;
pageNOs = c.toString();
flag = true;
}
}
return pageNOs;
}
}
On Fri, May 21, 2010 at 4:54 AM, RivasAviles, Miguel
<[email protected]> wrote:
Thank you Eric for pointing me on the right direction.
I put couple of hours with the approach you told me and was able to rotate the
page (270) but now I have it upside down.
But looking for more answers on the web seems to me like I opened a pandora's
box... people getting angry when talking about that matrix... I'm even scare
to ask about it :)
Any ways thank you for your answer. I think I'll have to go back to my old
math books... man more that 15 years without touching that!
Thanks.
________________________________________
From: de Beus, Eric [[email protected]]
Sent: Thursday, May 20, 2010 11:06 AM
To: Post all your questions about iText here
Subject: Re: [iText-questions] Document rotates while merging
Miguel,
Scanners often create rotated pages, because the scanner actually
creates an image with the scan lines running along the long edge
of the sheet, and then rotates the page to compensate. You need
to get the rotation of the pages from the PdfReader, and then
apply that rotation in your call to AddTemplate, using the overloaded
method that takes a transformation matrix.
- Eric
-----Original Message-----
From: RivasAviles, Miguel [mailto:[email protected]]
Sent: Thursday, May 20, 2010 8:44 AM
To: '[email protected]'
Subject: [iText-questions] Document rotates while merging
Hello,
I'm merging some pdf files using PdfReader but for some reason documents
that contains images (created from scanned documents) just rotates.
Please see attachments.
I'm using a simple merging process. Any Idea on what am I doing wrong?
foreach (PdfReader pdfReader in readers) {
// Create a new page in the target for each source page.
while (pageOfCurrentReaderPDF < pdfReader.NumberOfPages)
{
document.NewPage();
pageOfCurrentReaderPDF++;
currentPageNumber++;
page = writer.GetImportedPage(pdfReader,
pageOfCurrentReaderPDF);
cb.AddTemplate(page, 0, 0);
// Code for pagination.
if (paginate)
{
cb.BeginText();
cb.SetFontAndSize(bf, 9);
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER,
string.Format("{0} of {1}", currentPageNumber, totalPages), 520, 5, 0);
cb.EndText();
}
// Confidential notice
cb.BeginText();
cb.SetFontAndSize(bf, 30);
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT,
"CONFIDENTIAL", 600, 300, 90);
cb.EndText();
}
pageOfCurrentReaderPDF = 0;
}
Thank you.
Miguel
------------------------------------------------------------------------------
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://www.itextpdf.com/book/
Check the site with examples before you ask questions:
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/
------------------------------------------------------------------------------
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://www.itextpdf.com/book/
Check the site with examples before you ask questions:
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/
------------------------------------------------------------------------------
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://www.itextpdf.com/book/
Check the site with examples before you ask questions:
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/