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


Java PdfReader.getPageSize方法代碼示例

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


在下文中一共展示了PdfReader.getPageSize方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: testRotateAndZoomUpperHalfPage

import com.itextpdf.text.pdf.PdfReader; //導入方法依賴的package包/類
/**
 * <a href="http://stackoverflow.com/questions/35374110/how-do-i-use-itext-to-have-a-landscaped-pdf-on-half-of-a-a4-back-to-portrait-and">
 * How do i use iText to have a landscaped PDF on half of a A4 back to portrait and full size on A4
 * </a>
 * <p>
 * This sample shows how to rotate and enlarge the upper half of an A4 page to fit into a new A4 page.
 * </p>
 */
@Test
public void testRotateAndZoomUpperHalfPage() throws IOException, DocumentException
{
    try (   InputStream resource = getClass().getResourceAsStream("/mkl/testarea/itext5/extract/test.pdf");
            OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "test-upperHalf.pdf"))   )
    {
        PdfReader reader = new PdfReader(resource);
        Document document = new Document(PageSize.A4);
        PdfWriter writer = PdfWriter.getInstance(document, result);
        document.open();

        double sqrt2 = Math.sqrt(2);
        Rectangle pageSize = reader.getPageSize(1);
        PdfImportedPage importedPage = writer.getImportedPage(reader, 1);
        writer.getDirectContent().addTemplate(importedPage, 0, sqrt2, -sqrt2, 0, pageSize.getTop() * sqrt2, -pageSize.getLeft() * sqrt2);
        
        document.close();
    }
}
 
開發者ID:mkl-public,項目名稱:testarea-itext5,代碼行數:28,代碼來源:EnlargePagePart.java

示例3: createMultiUseIndirectTextPdf

import com.itextpdf.text.pdf.PdfReader; //導入方法依賴的package包/類
static byte[] createMultiUseIndirectTextPdf() throws DocumentException, IOException
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, baos);
    document.open();
    PdfReader reader = new PdfReader(createSimpleTextPdf());
    PdfImportedPage template = writer.getImportedPage(reader, 1);
    Rectangle pageSize = reader.getPageSize(1);
    writer.getDirectContent().addTemplate(template, 0, .7f, -.7f, 0, pageSize.getRight(), (pageSize.getTop() + pageSize.getBottom()) / 2);
    writer.getDirectContent().addTemplate(template, 0, .7f, -.7f, 0, pageSize.getRight(), pageSize.getBottom());
    document.newPage();
    writer.getDirectContent().addTemplate(template, pageSize.getLeft(), pageSize.getBottom());
    document.close();

    return baos.toByteArray();
}
 
開發者ID:mkl-public,項目名稱:testarea-itext5,代碼行數:19,代碼來源:SimpleRedactionTest.java

示例4: split

import com.itextpdf.text.pdf.PdfReader; //導入方法依賴的package包/類
void split(PdfReader reader, int page) throws IOException
{
    PdfImportedPage importedPage = writer.getImportedPage(reader, page);

    Rectangle pageSizeToImport = reader.getPageSize(page);
    Iterable<Rectangle> rectangles = determineSplitRectangles(reader, page);

    for (Rectangle rectangle : rectangles) {
        newPage(rectangle);
        PdfContentByte directContent = writer.getDirectContent();
        directContent.saveState();
        directContent.rectangle(rectangle.getLeft(), rectangle.getBottom(), rectangle.getWidth(), rectangle.getHeight());
        directContent.clip();
        directContent.newPath();

        writer.getDirectContent().addTemplate(importedPage, -pageSizeToImport.getLeft(), -pageSizeToImport.getBottom());

        directContent.restoreState();
    }
}
 
開發者ID:mkl-public,項目名稱:testarea-itext5,代碼行數:21,代碼來源:Abstract2DPdfPageSplittingTool.java

示例5: merge

import com.itextpdf.text.pdf.PdfReader; //導入方法依賴的package包/類
void merge(PdfReader reader, PdfReaderContentParser parser, int page) throws IOException
{
    TextMarginFinder finder = parser.processContent(page, new TextMarginFinder());
    Rectangle pageSizeToImport = reader.getPageSize(page);
    float heightToImport = finder.getHeight();
    float maxHeight = pageSize.getHeight() - topMargin - bottomMargin;
    if (heightToImport > maxHeight)
    {
        throw new IllegalArgumentException(String.format("Page %s content too large; height: %s, limit: %s.", page, heightToImport, maxHeight));
    }

    if (heightToImport > yPosition - pageSize.getBottom(bottomMargin))
    {
        newPage();
    }
    else if (!writer.isPageEmpty())
    {
        heightToImport += gap;
    }
    yPosition -= heightToImport;

    PdfImportedPage importedPage = writer.getImportedPage(reader, page);
    writer.getDirectContent().addTemplate(importedPage, 0, yPosition - (finder.getLly() - pageSizeToImport.getBottom()));
}
 
開發者ID:mkl-public,項目名稱:testarea-itext5,代碼行數:25,代碼來源:PdfDenseMergeTool.java

示例6: testSplitDocumentA6

import com.itextpdf.text.pdf.PdfReader; //導入方法依賴的package包/類
/**
 * <a href="https://stackoverflow.com/questions/46466747/how-to-split-a-pdf-page-in-java">
 * How to split a PDF page in java?
 * </a>
 * <p>
 * This test shows how to split the pages of a document into tiles of A6
 * size using the {@link Abstract2DPdfPageSplittingTool}.
 * </p>
 */
@Test
public void testSplitDocumentA6() throws IOException, DocumentException {
    try (InputStream resource = getClass().getResourceAsStream("document.pdf");
            OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "document-A6.pdf"))) {
        Abstract2DPdfPageSplittingTool tool = new Abstract2DPdfPageSplittingTool() {
            @Override
            protected Iterable<Rectangle> determineSplitRectangles(PdfReader reader, int page) {
                Rectangle targetSize = PageSize.A6;
                List<Rectangle> rectangles = new ArrayList<>();
                Rectangle pageSize = reader.getPageSize(page);
                for (float y = pageSize.getTop(); y > pageSize.getBottom() + 5; y-=targetSize.getHeight()) {
                    for (float x = pageSize.getLeft(); x < pageSize.getRight() - 5; x+=targetSize.getWidth()) {
                        rectangles.add(new Rectangle(x, y - targetSize.getHeight(), x + targetSize.getWidth(), y));
                    }
                }
                return rectangles;
            }
        };
        tool.split(result, new PdfReader(resource));
    }
}
 
開發者ID:mkl-public,項目名稱:testarea-itext5,代碼行數:31,代碼來源:SplitPages.java

示例7: testHideContenUnderRectangle

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 cover all content using a white rectangle.
 * </p>
 */
@Test
public void testHideContenUnderRectangle() throws IOException, DocumentException
{
    try (   InputStream resource = getClass().getResourceAsStream("document.pdf");
            OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "document-hiddenContent.pdf")))
    {
        PdfReader pdfReader = new PdfReader(resource);
        PdfStamper pdfStamper = new PdfStamper(pdfReader, result);
        for (int page = 1; page <= pdfReader.getNumberOfPages(); page++)
        {
            Rectangle pageSize = pdfReader.getPageSize(page);
            PdfContentByte canvas = pdfStamper.getOverContent(page);
            canvas.setColorFill(BaseColor.WHITE);
            canvas.rectangle(pageSize.getLeft(), pageSize.getBottom(), pageSize.getWidth(), pageSize.getHeight());
            canvas.fill();
        }
        pdfStamper.close();
    }
}
 
開發者ID:mkl-public,項目名稱:testarea-itext5,代碼行數:28,代碼來源:HideContent.java

示例8: testRedactLikeShiranSEkanayake

import com.itextpdf.text.pdf.PdfReader; //導入方法依賴的package包/類
/**
 * <a href="http://stackoverflow.com/questions/38278816/remove-header-of-a-pdf-using-itext-pdfcleanupprocessor-does-not-work">
 * Remove header of a pdf using iText PdfCleanUpProcessor does not work
 * </a>
 * <br/>
 * <a href="https://www.dropbox.com/s/4u8vupjqc4st3ib/love.pdf?dl=0">
 * love.pdf
 * </a>
 * <p>
 * Cannot reproduce, I get a <code>org.apache.commons.imaging.ImageReadException: Invalid marker found in entropy data</code>.
 * </p>
 */
@Test
public void testRedactLikeShiranSEkanayake() throws IOException, DocumentException
{
    try (   InputStream resource = getClass().getResourceAsStream("love.pdf");
            OutputStream result = new FileOutputStream(new File(OUTPUTDIR, "love-redacted.pdf")) )
    {
        PdfReader reader = new PdfReader(resource);
        PdfStamper stamper = new PdfStamper(reader, result);
        List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<PdfCleanUpLocation>();

        for(int i=1; i<=reader.getNumberOfPages(); i++)
        {
                //System.out.println(i);
                Rectangle mediabox = reader.getPageSize(i); 
                cleanUpLocations.add(new PdfCleanUpLocation(i, new Rectangle(0,800,1000,1000)));
        }
        PdfCleanUpProcessor cleaner = new PdfCleanUpProcessor(cleanUpLocations, stamper);
        cleaner.cleanUp();
        stamper.close();
        reader.close(); 
    }
}
 
開發者ID:mkl-public,項目名稱:testarea-itext5,代碼行數:35,代碼來源:RedactText.java

示例9: createRotatedIndirectTextPdf

import com.itextpdf.text.pdf.PdfReader; //導入方法依賴的package包/類
static byte[] createRotatedIndirectTextPdf() throws DocumentException, IOException
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, baos);
    document.open();
    PdfReader reader = new PdfReader(createSimpleTextPdf());
    PdfImportedPage template = writer.getImportedPage(reader, 1);
    Rectangle pageSize = reader.getPageSize(1);
    writer.getDirectContent().addTemplate(template, .7f, .7f, -.7f, .7f, 400, -200);
    document.newPage();
    writer.getDirectContent().addTemplate(template, pageSize.getLeft(), pageSize.getBottom());
    document.close();

    return baos.toByteArray();
}
 
開發者ID:mkl-public,項目名稱:testarea-itext5,代碼行數:18,代碼來源:SimpleRedactionTest.java

示例10: split

import com.itextpdf.text.pdf.PdfReader; //導入方法依賴的package包/類
void split(PdfReader reader, int page) throws IOException
{
    PdfImportedPage importedPage = writer.getImportedPage(reader, page);
    PdfContentByte directContent = writer.getDirectContent();
    yPosition = pageSize.getTop();

    Rectangle pageSizeToImport = reader.getPageSize(page);
    float[] borderPositions = determineSplitPositions(reader, page);
    if (borderPositions == null || borderPositions.length < 2)
        return;

    for (int borderIndex = 0; borderIndex + 1 < borderPositions.length; borderIndex++) {
        float height = borderPositions[borderIndex] - borderPositions[borderIndex + 1];
        if (height <= 0)
            continue;

        directContent.saveState();
        directContent.rectangle(0, yPosition - height, pageSizeToImport.getWidth(), height);
        directContent.clip();
        directContent.newPath();

        writer.getDirectContent().addTemplate(importedPage, 0, yPosition - (borderPositions[borderIndex] - pageSizeToImport.getBottom()));

        directContent.restoreState();
        newPage();
    }
}
 
開發者ID:mkl-public,項目名稱:testarea-itext5,代碼行數:28,代碼來源:AbstractPdfPageSplittingTool.java

示例11: testWithWriter

import com.itextpdf.text.pdf.PdfReader; //導入方法依賴的package包/類
@Test
public void testWithWriter() throws DocumentException, IOException
{
    InputStream resourceStream = getClass().getResourceAsStream("test.pdf");
    try
    {
        PdfReader reader = new PdfReader(resourceStream);
        Rectangle pageSize = reader.getPageSize(1);

        Rectangle rect = getOutputPageSize(pageSize, reader, 1);

        Document document = new Document(rect, 0, 0, 0, 0);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(RESULT_FOLDER, "test-trimmed-writer.pdf")));

        document.open();
        PdfImportedPage page;

        // Go through all pages
        int n = reader.getNumberOfPages();
        for (int i = 1; i <= n; i++)
        {
            document.newPage();
            page = writer.getImportedPage(reader, i);
            System.out.println("BBox:  "+ page.getBoundingBox().toString());
            Image instance = Image.getInstance(page);
            document.add(instance);
            Rectangle outputPageSize = document.getPageSize();
            System.out.println(outputPageSize.toString());
        }
        document.close();
    }
    finally
    {
        if (resourceStream != null)
            resourceStream.close();
    }
}
 
開發者ID:mkl-public,項目名稱:testarea-itext5,代碼行數:38,代碼來源:TestTrimPdfPage.java

示例12: 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

示例13: 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

示例14: 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

示例15: 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


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