However, there's one problem with that code. The code is fine if you combined pdfs of the same size. But as in my case where I have pdfs of both portrait and landscape, landscape pdfs are cut off. Originally I thought the way to solve this problem is rotating those landscape pdfs and then merge. However, rotating pdf works only when you output the merged pdf using a PdfStamper. The code uses PdfWriter which takes a Document object to write the pdf contents.
After several hours of googling the solution, I realized that, the solution is not by rotating the pdf, but buy changing pdf size. When document.newPage() is called it's generating a blank page and contents are being written to the new page in the following lines of code. The default page size is portrait size, so if we change the page size and copied over the content, the new PDF would be copied without being chopped off. Only one line of code is needed to solve the problem, which is
document.setPageSize(pdfReader.getPageSize(currentPageNumber))
Here pdfReader must be pointing the current pdf that is being copied over. Another important thing to note is that setPageSize() method has to be put before document.newPage(). If you put it after, then the changed page size will be effective only for the next page, not the current one.
How to convert pdf to jpeg/jpg in ASP.NET using PDF-to-JPEG Image Conversion Component for .NET
ReplyDelete