【问题标题】:Rotate single page of document旋转单页文档
【发布时间】:2011-05-27 17:50:18
【问题描述】:

当我使用 iText 时,如何旋转我的 PdF 的第二页。

我希望保持相同方向的第一页和其他页面。

我知道……

Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);

但这会旋转一切。

【问题讨论】:

    标签: java itext


    【解决方案1】:

    您可以在document.addNew(); 之前使用document.setPageSize()

    例如:

    Document document = new Document();
    ....
    document.setPageSize(PageSie.A4);
    document.newPage();
    ......
    
    document.setPageSize(PageSize.A4.rotate());
    document.newPage();
    

    对我来说效果很好。

    【讨论】:

      【解决方案2】:

      来自http://itextpdf.com/examples/iia.php?id=232

      /*
       * This class is part of the book "iText in Action - 2nd Edition"
       * written by Bruno Lowagie (ISBN: 9781935182610)
       * For more info, go to: http://itextpdf.com/examples/
       * This example only works with the AGPL version of iText.
       */
      
      package part4.chapter13;
      
      import java.io.FileOutputStream;
      import java.io.IOException;
      
      import part1.chapter03.MovieTemplates;
      
      import com.itextpdf.text.DocumentException;
      import com.itextpdf.text.pdf.PdfDictionary;
      import com.itextpdf.text.pdf.PdfName;
      import com.itextpdf.text.pdf.PdfNumber;
      import com.itextpdf.text.pdf.PdfReader;
      import com.itextpdf.text.pdf.PdfStamper;
      
      public class RotatePages {
      
          /** The resulting PDF. */
          public static final String RESULT
              = "results/part4/chapter13/timetable_rotated.pdf";
      
          /**
           * Manipulates a PDF file src with the file dest as result
           * @param src the original PDF
           * @param dest the resulting PDF
           * @throws IOException
           * @throws DocumentException
           */
          public void manipulatePdf(String src, String dest)
              throws IOException, DocumentException {
              PdfReader reader = new PdfReader(MovieTemplates.RESULT);
              int n = reader.getNumberOfPages();
              int rot;
              PdfDictionary pageDict;
              for (int i = 1; i <= n; i++) {
                  rot = reader.getPageRotation(i);
                  pageDict = reader.getPageN(i);
                  pageDict.put(PdfName.ROTATE, new PdfNumber(rot + 90));
              }
              PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
              stamper.close();
              reader.close();
          }
      
          /**
           * Main method creating the PDF.
           * @param    args    no arguments needed
           * @throws DocumentException 
           * @throws IOException 
           */
          public static void main(String[] args)
              throws IOException, DocumentException {
              new MovieTemplates().createPdf(MovieTemplates.RESULT);
              new RotatePages().manipulatePdf(MovieTemplates.RESULT, RESULT);
          }
      }
      

      【讨论】:

        【解决方案3】:

        我旋转方向:

        PdfWriter writer = new PdfWriter(out);
        PdfDocument pdf = new PdfDocument(writer);
        Document document = new Document(pdf, PageSize.LETTER.rotate());
        

        【讨论】:

        • 这不是操作员所说的他所知道的,但与他想要的相反将旋转所有内容而不仅仅是给定的页面?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-01
        • 1970-01-01
        • 2020-08-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多