当前位置: 首页>>代码示例>>Java>>正文


Java PdfCopy.addPage方法代码示例

本文整理汇总了Java中com.itextpdf.text.pdf.PdfCopy.addPage方法的典型用法代码示例。如果您正苦于以下问题:Java PdfCopy.addPage方法的具体用法?Java PdfCopy.addPage怎么用?Java PdfCopy.addPage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.itextpdf.text.pdf.PdfCopy的用法示例。


在下文中一共展示了PdfCopy.addPage方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: split

import com.itextpdf.text.pdf.PdfCopy; //导入方法依赖的package包/类
public List<byte[]> split(byte[] input) throws IOException, DocumentException {
    PdfReader pdfReader = new PdfReader(input);
    List<byte[]> pdfFiles = new ArrayList<>();
    int pageCount = pdfReader.getNumberOfPages();
    int pageIndex = 0;
    while (++pageIndex <= pageCount) {
        Document document = new Document(pdfReader.getPageSizeWithRotation(pageIndex));
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        PdfCopy pdfCopy = new PdfSmartCopy(document, byteArrayOutputStream);
        pdfCopy.setFullCompression();
        PdfImportedPage pdfImportedPage = pdfCopy.getImportedPage(pdfReader, pageIndex);
        document.open();
        pdfCopy.addPage(pdfImportedPage);
        document.close();
        pdfCopy.close();
        pdfFiles.add(byteArrayOutputStream.toByteArray());
    }
    return pdfFiles;
}
 
开发者ID:mkl-public,项目名称:testarea-itext5,代码行数:20,代码来源:SplitSmart.java

示例2: ExtractPages

import com.itextpdf.text.pdf.PdfCopy; //导入方法依赖的package包/类
public static byte[] ExtractPages(String pdfDocument, int startPage, int endPage) throws IOException, DocumentException
{
    try (InputStream pdfDocumentStream = SmartMerging.class.getResourceAsStream(pdfDocument))
    {
        PdfReader reader = new PdfReader(pdfDocumentStream);
        int numberOfPages = reader.getNumberOfPages();
        int endPageResolved = endPage > 0 ? endPage : numberOfPages;
        if (startPage > numberOfPages || endPageResolved > numberOfPages)
            System.err.printf("Error: page indices (%s, %s) out of bounds. Document has {2} pages.", startPage, endPageResolved, numberOfPages);

        byte[] outputDocument;
        try (ByteArrayOutputStream msOut = new ByteArrayOutputStream())
        {
            Document doc = new Document();
            PdfCopy pdfCopyProvider = new PdfCopy(doc, msOut);
            doc.open();
            for (int i = startPage; i <= endPageResolved; i++)
            {
                PdfImportedPage page = pdfCopyProvider.getImportedPage(reader, i);
                pdfCopyProvider.addPage(page);
            }
            doc.close();
            reader.close();
            outputDocument = msOut.toByteArray();
        }

        return outputDocument;
    }
}
 
开发者ID:mkl-public,项目名称:testarea-itext5,代码行数:30,代码来源:SmartMerging.java

示例3: splitIntoHalfPages

import com.itextpdf.text.pdf.PdfCopy; //导入方法依赖的package包/类
/**
 * This methods creates a copy of the source document containing each page twice,
 * once with the cropbox limited to the left half page, once to the right one.
 */
void splitIntoHalfPages(InputStream source, File target) throws IOException, DocumentException
{
    final PdfReader reader = new PdfReader(source);
    
    try (   OutputStream targetStream = new FileOutputStream(target)    )
    {
        Document document = new Document();
        PdfCopy copy = new PdfCopy(document, targetStream);
        document.open();

        for (int page = 1; page <= reader.getNumberOfPages(); page++)
        {
            PdfDictionary pageN = reader.getPageN(page);
            Rectangle cropBox = reader.getCropBox(page);
            PdfArray leftBox = new PdfArray(new float[]{cropBox.getLeft(), cropBox.getBottom(), (cropBox.getLeft() + cropBox.getRight()) / 2.0f, cropBox.getTop()});
            PdfArray rightBox = new PdfArray(new float[]{(cropBox.getLeft() + cropBox.getRight()) / 2.0f, cropBox.getBottom(), cropBox.getRight(), cropBox.getTop()});

            PdfImportedPage importedPage = copy.getImportedPage(reader, page);
            pageN.put(PdfName.CROPBOX, leftBox);
            copy.addPage(importedPage);
            pageN.put(PdfName.CROPBOX, rightBox);
            copy.addPage(importedPage);
        }
        
        document.close();
    }
    finally
    {
        reader.close();
    }
}
 
开发者ID:mkl-public,项目名称:testarea-itext5,代码行数:36,代码来源:SplitIntoHalfPages.java

示例4: testMergeLikeKishoreSagar

import com.itextpdf.text.pdf.PdfCopy; //导入方法依赖的package包/类
/**
   * <a href="https://stackoverflow.com/questions/46120706/when-i-open-a-pdf-in-adobe-acrobat-pro-dc-the-text-is-getting-messed-up-for-pdf">
   * when i open a pdf in adobe acrobat pro Dc the text is getting messed up for pdf creation i used itext5.5.12
   * </a>
   * <p>
   * Test files "1 Loyds.pdf", "2 CRPWLI.pdf", "3 SLC Dec.pdf", "4 Schedule.pdf",
   * and "5 Sched of INS.pdf" were received via e-mail from Kishore Rachakonda
   * ([email protected]) on 2017-09-11 17:31.
   * </p>
   * <p>
   * This test is a port of the Ruby-on-Rails merge routine provided by the OP.
   * The result does not have the problem described by the OP.
   * </p>
   * <p>
   * Later it became clear that the OP's merge result was post-processed by at
   * least two other programs, and one of those post processors appears to have
   * optimized the use of embedded fonts. Unfortunately "5 Sched of INS.pdf" is
   * not a completely valid PDF: It uses an embedded subset of a font but does
   * not mark the font name accordingly. Thus, the optimizing post processor
   * added this mere font subset (assuming it to be the whole font program) to
   * font resources which require glyphs missing in the subset.
   * </p> 
   */
  @Test
  public void testMergeLikeKishoreSagar() throws IOException, DocumentException {
      try (   InputStream resource1 = getClass().getResourceAsStream("1 Loyds.pdf");
              InputStream resource2 = getClass().getResourceAsStream("2 CRPWLI.pdf");
              InputStream resource3 = getClass().getResourceAsStream("3 SLC Dec.pdf");
              InputStream resource4 = getClass().getResourceAsStream("4 Schedule.pdf");
              InputStream resource5 = getClass().getResourceAsStream("5 Sched of INS.pdf");
              OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "mergeLikeKishoreSagar.pdf"))) {
          InputStream[] pdf_files = {resource1, resource2, resource3, resource4, resource5};

          Document doc = new Document();
          PdfCopy pdf_copy = new PdfCopy(doc, result);
          doc.open();
          
          for (InputStream pdf : pdf_files) {
              PdfReader reader = new PdfReader(pdf);
              int pages = reader.getNumberOfPages();
              for (int p = 1; p <= pages; p++)
                  pdf_copy.addPage(pdf_copy.getImportedPage(reader, p));
              reader.close();
          }

          doc.close();
      }

      /* ported from the original:
doc =Document.new
pdf_copy = PdfCopy.new(doc, FileStream.new(@output_filename))
doc.open
@pdf_files.each do |pdf|
  reader = PdfReader.new(pdf)
  pages = reader.getNumberOfPages()
  (1..pages).each do |p|
    pdf_copy.addPage(pdf_copy.getImportedPage(reader, p))
  end
  reader.close
end
doc.close
       */
  }
 
开发者ID:mkl-public,项目名称:testarea-itext5,代码行数:64,代码来源:Merging.java

示例5: testSandeepSinghHeaderTable

import com.itextpdf.text.pdf.PdfCopy; //导入方法依赖的package包/类
/**
    * <a href="http://stackoverflow.com/questions/29977927/table-header-in-pdf-getting-displayed-using-itextpdf5-1-1-but-not-in-itextpdf5-5">
    * table header in pdf getting displayed using itextpdf5.1.1 but not in itextpdf5.5.3
    * </a>
    * <p>
    * Indeed, the code as presented by the OP does not show the header table. This makes sense, though:
    * </p>
    * <p>
    * The OP has cells with default padding (i.e. 2) and height 10, and he tries to insert text at height 7.
    * But 2 (top margin) + 7 (text height) + 2 (bottom margin) = 11, i.e. more than fits into the cell height 10.
    * Thus, the text does not fit and is not displayed.
    * </p>
    * <p>
    * You can fix this by either
    * <ul>
    * <li>using a smaller font, e.g. 6, or
    * <li>using a higher cell, e.g. 11, or
    * <li>using a smaller padding, e.g. 1, see below-
    * </p>
    */
@Test
public void testSandeepSinghHeaderTable() throws DocumentException, IOException
{
	byte[] strIntermediatePDFFile = createSampleDocument();
	String header1 = "Header 1";
	String header2 = "Header 2";
	String header3 = "Header 3";
	String header5 = "Header 5";
	

	Document document = new Document(PageSize.A4.rotate(), 20, 20, 75, 20);
	PdfCopy copy = new PdfCopy(document, new FileOutputStream(new File(RESULT_FOLDER, "stampTableHeader.pdf")));

	document.open();
	PdfReader pdfReaderIntermediate = new PdfReader(strIntermediatePDFFile);
	int numberOfPages = pdfReaderIntermediate.getNumberOfPages();
	Font ffont = new Font(Font.FontFamily.UNDEFINED, 7, Font.NORMAL);
	System.out.println("###### No. of Pages: " + numberOfPages);
	for (int j = 0; j < numberOfPages; )
	{
	    PdfImportedPage page = copy.getImportedPage(pdfReaderIntermediate, ++j);
	    PageStamp stamp = copy.createPageStamp(page);
	    Phrase footer = new Phrase(String.format("%d of %d", j, numberOfPages), ffont);
	    ColumnText.showTextAligned(stamp.getUnderContent(),
	                               Element.ALIGN_CENTER, footer,
	                               (document.right() - document.left()) /
	                               2 + document.leftMargin(),
	                               document.bottom() - 10, 0);
	    if (j != 1)
	    {
	    	PdfPTable headerTable = new PdfPTable(2);
	        headerTable.setTotalWidth(700);
	        headerTable.getDefaultCell().setFixedHeight(10);
	        headerTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
	        headerTable.getDefaultCell().setPadding(1); // Added!
	        headerTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
	        headerTable.addCell(new Phrase(String.format(header1), ffont));
	        headerTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
	        headerTable.addCell(new Phrase(String.format(header2), ffont));
	        headerTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
	        headerTable.addCell(new Phrase(String.format(header3), ffont));
	        headerTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
	        headerTable.addCell(new Phrase(String.format(header5, j), ffont));
	        headerTable.completeRow();
	        headerTable.writeSelectedRows(0, 5, 60.5f, 550, stamp.getUnderContent());
	    }

	    stamp.alterContents();
	    copy.addPage(page);
	}
	document.close();
}
 
开发者ID:mkl-public,项目名称:testarea-itext5,代码行数:73,代码来源:StampHeader.java

示例6: repairWithItext

import com.itextpdf.text.pdf.PdfCopy; //导入方法依赖的package包/类
/**
 * Makes a copy from each PDF-file in the folder and puts it in the same
 * folder with the prefix"Mig_iText"
 * 
 * @param takes
 *            in PdfReader and the filename as a string
 * @return: void
 * 
 */

@SuppressWarnings("rawtypes")
static void repairWithItext(PdfReader reader, File filename) throws DocumentException, IOException {

	Map<String, String> info = reader.getInfo();
	Document document = new Document();

	String filenameStr = filename.getName();

	PdfCopy copy = new PdfCopy(document, new FileOutputStream(examinedFolder + "//" + "Mig_iText" + filenameStr));

	int pdfVersion = filetools.pdf.PdfAnalysis.getPdfVersion(filename.toString());

	//TODO: But all the output PDF is PDF 1.4
	switch (pdfVersion) {
	case 2:
		copy.setPdfVersion(PdfWriter.PDF_VERSION_1_2);
		break;
	case 3:
		copy.setPdfVersion(PdfWriter.PDF_VERSION_1_3);
		break;
	case 4:
		copy.setPdfVersion(PdfWriter.PDF_VERSION_1_4);
		break;
	case 5:
		copy.setPdfVersion(PdfWriter.PDF_VERSION_1_5);
		break;
	case 6:
		copy.setPdfVersion(PdfWriter.PDF_VERSION_1_6);
		break;
	case 7:
		copy.setPdfVersion(PdfWriter.PDF_VERSION_1_7);
		break;
	}

	// TODO: there is a way to get all the metadata as described in one of
	// Bruno's books
	if (info.get("Title") != null)
		document.addTitle((String) info.get("Title"));
	if (info.get("Author") != null)
		document.addAuthor((String) info.get("Author"));
	if (info.get("Keywords") != null)
		document.addKeywords((String) info.get("Keywords"));
	// TODO: Is this the right Keyword?
	if (info.get("Date") != null)
		document.addKeywords((String) info.get("Date"));
	copy.createXmpMetadata();
	document.open();
	int n = reader.getNumberOfPages();
	for (int i = 0; i < n;) {
		copy.addPage(copy.getImportedPage(reader, ++i));
	}
	copy.freeReader(reader);
	document.close();
}
 
开发者ID:friesey,项目名称:preservation-tools,代码行数:65,代码来源:iTextRepairPdf.java

示例7: mergePdfs

import com.itextpdf.text.pdf.PdfCopy; //导入方法依赖的package包/类
public void mergePdfs() throws DocumentException, IOException {
	String title = book.getPdfTitle() + ".pdf";
	File saveFile = new File(saveFolder, title);

	int count = 1;
	while (saveFile.exists()) {
		title = book.getPdfTitle() + "_" + count++ + ".pdf";
		saveFile = new File(saveFolder, title);
	}
	book.setInfo("saveFile", saveFile.toString());

	Document document = new Document();
	PdfCopy destPdf = new PdfCopy(document, new FileOutputStream(saveFile));
	document.open();
	PdfReader reader;
	int page_offset = 0;
	int n;
	ArrayList<HashMap<String, Object>> bookmarks = new ArrayList<HashMap<String, Object>>();
	List<HashMap<String, Object>> tmp;

	count = 1;
	System.out.println("Start mergin\u2026");
	for (File srcPdf : src) {
		
		if (Thread.interrupted()) {
		    return;
		}
		
		System.out.print(":: " + count++ + "/" + src.size());
		reader = new PdfReader(srcPdf.toString());

		tmp = SimpleBookmark.getBookmark(reader);
		if (tmp != null) {
			SimpleBookmark.shiftPageNumbers(tmp, page_offset, null);
			bookmarks.addAll(tmp);
		}

		n = reader.getNumberOfPages();
		page_offset += n;
		for (int page = 0; page < n;) {
			destPdf.addPage(destPdf.getImportedPage(reader, ++page));
		}
		destPdf.freeReader(reader);
		reader.close();
		System.out.println(" succeed.");
	}
	if (!bookmarks.isEmpty()) {
		destPdf.setOutlines(bookmarks);
	}

	if (book.getInfo("author") != null)
		document.addAuthor(book.getInfo("author"));
	if (book.getInfo("title") != null)
		document.addTitle(book.getInfo("title"));
	if (book.getInfo("subtitle") != null)
		document.addSubject(book.getInfo("subtitle"));
	document.close();

	System.out.println("Merge complete. Saved to " + saveFile);
}
 
开发者ID:sgelb,项目名称:sldownloader,代码行数:61,代码来源:Pdf.java


注:本文中的com.itextpdf.text.pdf.PdfCopy.addPage方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。