java 如何在itext中将页面大小设置为欧洲A4

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6816496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:23:01  来源:igfitidea点击:

How set page size to European A4 in itext

javaitext

提问by Mehul

I am using the following code to merge pdf. But it is giving me a margin problem. It is leaving more margin on top, left & bottom & less margin in right then the original document. i tried to set margins also but it did not work. please help me out with this or can u help me set page size to European A4

我正在使用以下代码合并 pdf。但它给了我一个保证金问题。与原始文档相比,它在顶部、左侧和底部留下了更多的边距,而在右侧留下了更少的边距。我也尝试设置边距,但没有用。请帮我解决这个问题,或者你能帮我将页面大小设置为欧洲 A4

 public class Pdf_Merge {
  public static void main(String[] args) {
    try {
      List<InputStream> pdfs = new ArrayList<InputStream>();
      pdfs.add(new FileInputStream("file1.pdf"));
      pdfs.add(new FileInputStream("file2.pdf"));
      OutputStream output = new FileOutputStream("Output_file.pdf");
      System.out.println("Created");
      Pdf_Merge.concatPDFs(pdfs, output, true);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void concatPDFs(List<InputStream> streamOfPDFFiles, OutputStream    
  outputStream, boolean paginate) {

    Document document = new Document(PageSize.A4);
    try {
    List<InputStream> pdfs = streamOfPDFFiles;
    List<PdfReader> readers = new ArrayList<PdfReader>();
    int totalPages = 0;
    Iterator<InputStream> iteratorPDFs = pdfs.iterator();

    // Create Readers for the pdfs.
      while (iteratorPDFs.hasNext()) {
        InputStream pdf = iteratorPDFs.next();
        PdfReader pdfReader = new PdfReader(pdf);
        readers.add(pdfReader);
        totalPages += pdfReader.getNumberOfPages();
      }
      // Create a writer for the outputstream
      PdfWriter writer = PdfWriter.getInstance(document, outputStream);


      document.open();

      PdfContentByte cb = writer.getDirectContent(); // Holds the PDF
   // data

      PdfImportedPage page;
      int currentPageNumber = 0;
      int pageOfCurrentReaderPDF = 0;
      Iterator<PdfReader> iteratorPDFReader = readers.iterator();

  // Loop through the PDF files and add to the output.
      while (iteratorPDFReader.hasNext()) {
        PdfReader pdfReader = iteratorPDFReader.next();

    // Create a new page in the target for each source page.
        while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
          document.newPage();
          pageOfCurrentReaderPDF++;
          currentPageNumber++;
          page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
          cb.addTemplate(page, 0, 0);

       // Code for pagination.
          if (paginate) {
          cb.beginText();
          cb.endText();

         }
       }
        pageOfCurrentReaderPDF = 0;
     }
      outputStream.flush();

      document.close();
      outputStream.close();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (document.isOpen())
       document.close();
      try {
        if (outputStream != null)
         outputStream.close();
      } catch (IOException ioe) {
        ioe.printStackTrace();
      }
    }
  }
}

回答by Luixv

import com.lowagie.text.PageSize;

...    

final Document document = new Document(PageSize.A4);