使用iText插入pdf后,为什么纵向页面会更改为横向页面?

时间:2016-04-12 10:23:08

标签: java itext

使用" itext-5.5.8",尝试将(一)页肖像pdf插入主pdf文档,代码工作正常但插入肖像页面后自动更改为横向页面,不要&# 39;不知道为什么?

CODE:

try {
        PdfReader firstPdf = new PdfReader(mainFileWithPath); //main doc
        PdfReader secondPdf =new PdfReader(addFileNameWithPath); // inserting pages

        PdfStamper stamp = new PdfStamper(firstPdf, new FileOutputStream(outputPDFFile));

            int totalNumOfPagesToInsert = secondPdf.getNumberOfPages();
            int i =1;
            while (i<=totalNumOfPagesToInsert) {
                // Get a page(s) from secondPdf with the given pageNo
                PdfImportedPage page  = stamp.getImportedPage(secondPdf,i);

                // insert new page in to the newly created pdf at specified page number.
                stamp.insertPage(INSERT_AT_PAGE_NO + (i-1), secondPdf.getPageSize(i));

                // copy the content of the page copied from secondPdf.
                stamp.getUnderContent(INSERT_AT_PAGE_NO + (i-1)).addTemplate(page, 0, 0);

                i++;
            }

            //close the new created pdf.
            stamp.close(); 

请给我指示解决这个问题!感谢

1 个答案:

答案 0 :(得分:1)

由于作者@Bruno Lowagie提到&#34; 没有考虑到轮换&#34;

已修复问题,例如...... 以下代码 ...

try {

            PdfReader firstPdf = new PdfReader(mainFileWithPath);
            PdfReader secondPdf =new PdfReader(addFileNameWithPath);

            // create new pdf with the content from firstPdf
            PdfStamper stamp = new PdfStamper(firstPdf, new FileOutputStream(outputPDFFile));
            stamp.setRotateContents(false);

            int totalNumOfPagesToInsert = secondPdf.getNumberOfPages();
            int i =1;
            while (i<=totalNumOfPagesToInsert) {
                // Get a single page from secondPdf with the given pageNo
                PdfImportedPage page  = stamp.getImportedPage(secondPdf,i); //Actual working code

                // insert new page in to the newly created pdf at specified page number.
                // choose page size bas
                stamp.insertPage(INSERT_AT_PAGE_NO + (i-1), secondPdf.getPageSizeWithRotation(i)); //Actual working code

                // copy the content of the page copied from secondPdf.
                stamp.getUnderContent(INSERT_AT_PAGE_NO + (i-1)).addTemplate(page, 0, 0); //Actual working code

                i++;
            }

            //close the new created pdf.
            stamp.close();