當前位置: 首頁>>代碼示例>>Java>>正文


Java PdfReader.getPageN方法代碼示例

本文整理匯總了Java中com.itextpdf.text.pdf.PdfReader.getPageN方法的典型用法代碼示例。如果您正苦於以下問題:Java PdfReader.getPageN方法的具體用法?Java PdfReader.getPageN怎麽用?Java PdfReader.getPageN使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.itextpdf.text.pdf.PdfReader的用法示例。


在下文中一共展示了PdfReader.getPageN方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testSwitchOrientation

import com.itextpdf.text.pdf.PdfReader; //導入方法依賴的package包/類
/**
 * <a href="http://stackoverflow.com/questions/34394199/i-cant-rotate-my-page-from-existing-pdf">
 * I can't rotate my page from existing PDF
 * </a>
 * <p>
 * Switching between portrait and landscape like this obviously will cut off some parts of the page.
 * </p>
 */
@Test
public void testSwitchOrientation() throws DocumentException, IOException
{
    try (InputStream resourceStream = getClass().getResourceAsStream("/mkl/testarea/itext5/extract/n2013.00849449.pdf"))
    {
        PdfReader reader = new PdfReader(resourceStream);
        int n = reader.getNumberOfPages();
        PdfDictionary pageDict;
        for (int i = 1; i <= n; i++) {
            Rectangle rect = reader.getPageSize(i);
            Rectangle crop = reader.getCropBox(i);
            pageDict = reader.getPageN(i);
            pageDict.put(PdfName.MEDIABOX, new PdfArray(new float[] {rect.getBottom(), rect.getLeft(), rect.getTop(), rect.getRight()}));
            pageDict.put(PdfName.CROPBOX, new PdfArray(new float[] {crop.getBottom(), crop.getLeft(), crop.getTop(), crop.getRight()}));
        }
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(new File(RESULT_FOLDER, "n2013.00849449-switch.pdf")));
        stamper.close();
        reader.close();
    }
}
 
開發者ID:mkl-public,項目名稱:testarea-itext5,代碼行數:29,代碼來源:SwitchPageCanvas.java

示例2: testRemoveContent

import com.itextpdf.text.pdf.PdfReader; //導入方法依賴的package包/類
/**
 * <a href="http://stackoverflow.com/questions/43870545/filling-a-pdf-with-itextsharp-and-then-hiding-the-base-layer">
 * Filling a PDF with iTextsharp and then hiding the base layer
 * </a>
 * <p>
 * This test shows how to remove all content.
 * </p>
 */
@Test
public void testRemoveContent() throws IOException, DocumentException
{
    try (   InputStream resource = getClass().getResourceAsStream("document.pdf");
            OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "document-removedContent.pdf")))
    {
        PdfReader pdfReader = new PdfReader(resource);
        for (int page = 1; page <= pdfReader.getNumberOfPages(); page++)
        {
            PdfDictionary pageDictionary = pdfReader.getPageN(page);
            pageDictionary.remove(PdfName.CONTENTS);
        }
        new PdfStamper(pdfReader, result).close();
    }
}
 
開發者ID:mkl-public,項目名稱:testarea-itext5,代碼行數:24,代碼來源:HideContent.java

示例3: editPage

import com.itextpdf.text.pdf.PdfReader; //導入方法依賴的package包/類
/**
 * This method edits the immediate contents of a page, i.e. its content stream.
 * It explicitly does not descent into form xobjects, patterns, or annotations.
 */
public void editPage(PdfStamper pdfStamper, int pageNum) throws IOException
{
    PdfReader pdfReader = pdfStamper.getReader();
    PdfDictionary page = pdfReader.getPageN(pageNum);
    byte[] pageContentInput = ContentByteUtils.getContentBytesForPage(pdfReader, pageNum);
    page.remove(PdfName.CONTENTS);
    editContent(pageContentInput, page.getAsDict(PdfName.RESOURCES), pdfStamper.getUnderContent(pageNum));
}
 
開發者ID:mkl-public,項目名稱:testarea-itext5,代碼行數:13,代碼來源:PdfContentStreamEditor.java

示例4: testMarkAnnotationsReadOnly

import com.itextpdf.text.pdf.PdfReader; //導入方法依賴的package包/類
/**
 * <a href="http://stackoverflow.com/questions/37275267/how-to-make-pdf-annotation-as-read-only-using-itext">
 * how to make pdf annotation as read only using itext?
 * </a>
 * <br/>
 * test-annotated.pdf <i>simple PDF with sticky note</i>
 * 
 * <p>
 * This test shows how to set the read-only flags of all annotations of a document.
 * </p>
 */
@Test
public void testMarkAnnotationsReadOnly() throws IOException, DocumentException
{
    try (   InputStream resourceStream = getClass().getResourceAsStream("test-annotated.pdf");
            OutputStream outputStream = new FileOutputStream(new File(RESULT_FOLDER, "test-annotated-ro.pdf"))    )
    {
        PdfReader reader = new PdfReader(resourceStream);
        PdfStamper stamper = new PdfStamper(reader, outputStream);

        for (int page = 1; page <= reader.getNumberOfPages(); page++)
        {
            PdfDictionary pageDictionary = reader.getPageN(page);
            PdfArray annotationArray = pageDictionary.getAsArray(PdfName.ANNOTS);
            if (annotationArray == null)
                continue;
            for (PdfObject object : annotationArray)
            {
                PdfObject directObject = PdfReader.getPdfObject(object);
                if (directObject instanceof PdfDictionary)
                {
                    PdfDictionary annotationDictionary = (PdfDictionary) directObject;
                    PdfNumber flagsNumber = annotationDictionary.getAsNumber(PdfName.F);
                    int flags = flagsNumber != null ? flagsNumber.intValue() : 0;
                    flags |= PdfAnnotation.FLAGS_READONLY;
                    annotationDictionary.put(PdfName.F, new PdfNumber(flags));
                }
            }
        }

        stamper.close();
    }
}
 
開發者ID:mkl-public,項目名稱:testarea-itext5,代碼行數:44,代碼來源:MarkAnnotationReadOnly.java

示例5: testWithStamper

import com.itextpdf.text.pdf.PdfReader; //導入方法依賴的package包/類
@Test
public void testWithStamper() throws DocumentException, IOException
{
    InputStream resourceStream = getClass().getResourceAsStream("test.pdf");
    try
    {
        PdfReader reader = new PdfReader(resourceStream);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(new File(RESULT_FOLDER, "test-trimmed-stamper.pdf")));
        
        // Go through all pages
        int n = reader.getNumberOfPages();
        for (int i = 1; i <= n; i++)
        {
            Rectangle pageSize = reader.getPageSize(i);
            Rectangle rect = getOutputPageSize(pageSize, reader, i);

            PdfDictionary page = reader.getPageN(i);
            page.put(PdfName.CROPBOX, new PdfArray(new float[]{rect.getLeft(), rect.getBottom(), rect.getRight(), rect.getTop()}));
            stamper.markUsed(page);
        }
        stamper.close();
    }
    finally
    {
        if (resourceStream != null)
            resourceStream.close();
    }
}
 
開發者ID:mkl-public,項目名稱:testarea-itext5,代碼行數:29,代碼來源:TestTrimPdfPage.java

示例6: testWithStamperTopBottom

import com.itextpdf.text.pdf.PdfReader; //導入方法依賴的package包/類
@Test
public void testWithStamperTopBottom() throws DocumentException, IOException
{
    InputStream resourceStream = getClass().getResourceAsStream("test.pdf");
    try
    {
        PdfReader reader = new PdfReader(resourceStream);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(new File(RESULT_FOLDER, "test-trimmed-stamper-top-bottom.pdf")));
        
        // Go through all pages
        int n = reader.getNumberOfPages();
        for (int i = 1; i <= n; i++)
        {
            Rectangle pageSize = reader.getPageSize(i);
            Rectangle rect = getOutputPageSize2(pageSize, reader, i);

            PdfDictionary page = reader.getPageN(i);
            page.put(PdfName.CROPBOX, new PdfArray(new float[]{rect.getLeft(), rect.getBottom(), rect.getRight(), rect.getTop()}));
            stamper.markUsed(page);
        }
        stamper.close();
    }
    finally
    {
        if (resourceStream != null)
            resourceStream.close();
    }
}
 
開發者ID:mkl-public,項目名稱:testarea-itext5,代碼行數:29,代碼來源:TestTrimPdfPage.java

示例7: testWithStamperCentered

import com.itextpdf.text.pdf.PdfReader; //導入方法依賴的package包/類
@Test
public void testWithStamperCentered() throws DocumentException, IOException
{
    InputStream resourceStream = getClass().getResourceAsStream("test.pdf");
    try
    {
        PdfReader reader = new PdfReader(resourceStream);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(new File(RESULT_FOLDER, "test-trimmed-stamper-centered.pdf")));
        
        // Go through all pages
        int n = reader.getNumberOfPages();
        for (int i = 1; i <= n; i++)
        {
            Rectangle pageSize = reader.getPageSize(i);
            Rectangle rect = getOutputPageSize3(pageSize, reader, i);

            PdfDictionary page = reader.getPageN(i);
            page.put(PdfName.CROPBOX, new PdfArray(new float[]{rect.getLeft(), rect.getBottom(), rect.getRight(), rect.getTop()}));
            stamper.markUsed(page);
        }
        stamper.close();
    }
    finally
    {
        if (resourceStream != null)
            resourceStream.close();
    }
}
 
開發者ID:mkl-public,項目名稱:testarea-itext5,代碼行數:29,代碼來源:TestTrimPdfPage.java

示例8: testWithStamperExtFinder

import com.itextpdf.text.pdf.PdfReader; //導入方法依賴的package包/類
@Test
public void testWithStamperExtFinder() throws DocumentException, IOException
{
    InputStream resourceStream = getClass().getResourceAsStream("test.pdf");
    try
    {
        PdfReader reader = new PdfReader(resourceStream);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(new File(RESULT_FOLDER, "test-trimmed-stamper-ext.pdf")));
        
        // Go through all pages
        int n = reader.getNumberOfPages();
        for (int i = 1; i <= n; i++)
        {
            Rectangle pageSize = reader.getPageSize(i);
            Rectangle rect = getOutputPageSize4(pageSize, reader, i);

            PdfDictionary page = reader.getPageN(i);
            page.put(PdfName.CROPBOX, new PdfArray(new float[]{rect.getLeft(), rect.getBottom(), rect.getRight(), rect.getTop()}));
            stamper.markUsed(page);
        }
        stamper.close();
    }
    finally
    {
        if (resourceStream != null)
            resourceStream.close();
    }
}
 
開發者ID:mkl-public,項目名稱:testarea-itext5,代碼行數:29,代碼來源:TestTrimPdfPage.java

示例9: splitIntoHalfPages

import com.itextpdf.text.pdf.PdfReader; //導入方法依賴的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

示例10: cropMultipliedFile

import com.itextpdf.text.pdf.PdfReader; //導入方法依賴的package包/類
private static void cropMultipliedFile(File source, CropJob cropJob) throws FileNotFoundException, DocumentException,
		IOException {

	PdfReader reader = new PdfReader(source.getAbsolutePath());
	PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(cropJob.getDestinationFile()));
	stamper.setMoreInfo(cropJob.getSourceMetaInfo());

	PdfDictionary pageDict;
	int newPageNumber = 1;
	for (int origPageNumber = 1; origPageNumber <= cropJob.getSourcePageCount(); origPageNumber++) {
		SingleCluster cluster = cropJob.getClusterCollection().getSingleCluster(origPageNumber);

		// if no crop was selected do nothing
		if (cluster.getRatiosList().size() == 0) {
			newPageNumber++;
			continue;
		}

		for (Float[] ratios : cluster.getRatiosList()) {

			pageDict = reader.getPageN(newPageNumber);

			List<Rectangle> boxes = new ArrayList<Rectangle>();
			boxes.add(reader.getBoxSize(newPageNumber, "media"));
			boxes.add(reader.getBoxSize(newPageNumber, "crop"));
			int rotation = reader.getPageRotation(newPageNumber);

			Rectangle scaledBox = calculateScaledRectangle(boxes, ratios, rotation);

			PdfArray scaleBoxArray = new PdfArray();
			scaleBoxArray.add(new PdfNumber(scaledBox.getLeft()));
			scaleBoxArray.add(new PdfNumber(scaledBox.getBottom()));
			scaleBoxArray.add(new PdfNumber(scaledBox.getRight()));
			scaleBoxArray.add(new PdfNumber(scaledBox.getTop()));

			pageDict.put(PdfName.CROPBOX, scaleBoxArray);
			pageDict.put(PdfName.MEDIABOX, scaleBoxArray);
			// increment the pagenumber
			newPageNumber++;
		}
		int[] range = new int[2];
		range[0] = newPageNumber - 1;
		range[1] = cropJob.getSourcePageCount() + (newPageNumber - origPageNumber);
		SimpleBookmark.shiftPageNumbers(cropJob.getSourceBookmarks(), cluster.getRatiosList().size() - 1, range);
	}
	stamper.setOutlines(cropJob.getSourceBookmarks());
	stamper.close();
	reader.close();
}
 
開發者ID:mbaeuerle,項目名稱:Briss-2.0,代碼行數:50,代碼來源:CropManager.java

示例11: cropMultipliedFile

import com.itextpdf.text.pdf.PdfReader; //導入方法依賴的package包/類
private static void cropMultipliedFile(final CropDefinition cropDefinition, final File multipliedDocument,
		final PdfMetaInformation pdfMetaInformation) throws DocumentException, IOException {

	PdfReader reader = new PdfReader(multipliedDocument.getAbsolutePath());

	PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(cropDefinition.getDestinationFile()));
	stamper.setMoreInfo(pdfMetaInformation.getSourceMetaInfo());

	PdfDictionary pageDict;
	int newPageNumber = 1;

	for (int sourcePageNumber = 1; sourcePageNumber <= pdfMetaInformation.getSourcePageCount(); sourcePageNumber++) {

		List<Float[]> rectangleList = cropDefinition.getRectanglesForPage(sourcePageNumber);

		// if no crop was selected do nothing
		if (rectangleList.isEmpty()) {
			newPageNumber++;
			continue;
		}

		for (Float[] ratios : rectangleList) {

			pageDict = reader.getPageN(newPageNumber);

			List<Rectangle> boxes = new ArrayList<Rectangle>();
			boxes.add(reader.getBoxSize(newPageNumber, "media"));
			boxes.add(reader.getBoxSize(newPageNumber, "crop"));
			int rotation = reader.getPageRotation(newPageNumber);

			Rectangle scaledBox = RectangleHandler.calculateScaledRectangle(boxes, ratios, rotation);

			PdfArray scaleBoxArray = createScaledBoxArray(scaledBox);

			pageDict.put(PdfName.CROPBOX, scaleBoxArray);
			pageDict.put(PdfName.MEDIABOX, scaleBoxArray);
			// increment the pagenumber
			newPageNumber++;
		}
		int[] range = new int[2];
		range[0] = newPageNumber - 1;
		range[1] = pdfMetaInformation.getSourcePageCount() + (newPageNumber - sourcePageNumber);
		SimpleBookmark.shiftPageNumbers(pdfMetaInformation.getSourceBookmarks(), rectangleList.size() - 1, range);
	}
	stamper.setOutlines(pdfMetaInformation.getSourceBookmarks());
	stamper.close();
	reader.close();
}
 
開發者ID:mbaeuerle,項目名稱:Briss-2.0,代碼行數:49,代碼來源:DocumentCropper.java

示例12: cropPdf

import com.itextpdf.text.pdf.PdfReader; //導入方法依賴的package包/類
/**
 * <p>
 * This method restricts the media boxes of the pages in the given {@link PdfReader}
 * to the actual content found by the {@link MarginFinder} extended render listener.
 * </p>
 * <p>
 * It essentially is copied from the {@link TestTrimPdfPage} methods
 * {@link TestTrimPdfPage#testWithStamperExtFinder()} and
 * {@link TestTrimPdfPage#getOutputPageSize4(Rectangle, PdfReader, int)}.
 * In contrast to the code there this method manipulates
 * the media box because this is the only box respected by
 * {@link PdfWriter#getImportedPage(PdfReader, int)}.
 * </p>
 */
static void cropPdf(PdfReader reader) throws IOException
{
    int n = reader.getNumberOfPages();
    for (int i = 1; i <= n; i++)
    {
        PdfReaderContentParser parser = new PdfReaderContentParser(reader);
        MarginFinder finder = parser.processContent(i, new MarginFinder());
        Rectangle rect = new Rectangle(finder.getLlx(), finder.getLly(), finder.getUrx(), finder.getUry());

        PdfDictionary page = reader.getPageN(i);
        page.put(PdfName.MEDIABOX, new PdfArray(new float[]{rect.getLeft(), rect.getBottom(), rect.getRight(), rect.getTop()}));
    }
}
 
開發者ID:mkl-public,項目名稱:testarea-itext5,代碼行數:28,代碼來源:ImportPageWithoutFreeSpace.java

示例13: testExtractImageLikeSteveB

import com.itextpdf.text.pdf.PdfReader; //導入方法依賴的package包/類
/**
 * <a href="https://stackoverflow.com/questions/47101222/how-to-decode-image-with-asciihexdecode">
 * How to decode image with /ASCIIHexDecode
 * </a>
 * <br/>
 * <a href="https://1drv.ms/b/s!AjcEvFO-aWLMkbtXNVl_rmUXv6nnBQ">
 * test.pdf
 * </a>
 * <p>
 * The issue can be reproduced. Without studying the format
 * of the integrated image in detail, though, it is hard to
 * tell whether this is a bug in iText or a bug in the PDF.
 * </p>
 */
@Test
public void testExtractImageLikeSteveB() throws IOException
{
    try (InputStream resource = getClass().getResourceAsStream("testSteveB.pdf")) {
        PdfReader reader = new PdfReader(resource);
        for (int pageNumber = 1; pageNumber <= reader.getNumberOfPages(); pageNumber++)
        {
            PdfDictionary dictionary = reader.getPageN(pageNumber);
            FindImages(reader, dictionary);
        }
    }
}
 
開發者ID:mkl-public,項目名稱:testarea-itext5,代碼行數:27,代碼來源:ImageExtraction.java


注:本文中的com.itextpdf.text.pdf.PdfReader.getPageN方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。