Java Code Examples for com.itextpdf.text.pdf.PdfStamper#markUsed()

The following examples show how to use com.itextpdf.text.pdf.PdfStamper#markUsed() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestTrimPdfPage.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
@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();
    }
}
 
Example 2
Source File: TestTrimPdfPage.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
@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();
    }
}
 
Example 3
Source File: TestTrimPdfPage.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
@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();
    }
}
 
Example 4
Source File: TestTrimPdfPage.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
@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();
    }
}
 
Example 5
Source File: InsertPage.java    From testarea-itext5 with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/28911509/how-to-retain-page-labels-when-concatenating-an-existing-pdf-with-a-pdf-created">
 * How to retain page labels when concatenating an existing pdf with a pdf created from scratch?
 * </a>
 * <p>
 * A proposal how to implement the task using a {@link PdfStamper}.
 */
@Test
public void testInsertTitlePage() throws IOException, DocumentException
{
    try (   InputStream documentStream = getClass().getResourceAsStream("Labels.pdf");
            InputStream titleStream = getClass().getResourceAsStream("Cover.pdf");
            OutputStream outputStream = new FileOutputStream(new File(RESULT_FOLDER, "labels-with-cover-page.pdf"))    )
    {
        PdfReader titleReader = new PdfReader(titleStream);
        PdfReader reader = new PdfReader(documentStream);
        PdfStamper stamper = new PdfStamper(reader, outputStream);

        PdfImportedPage page = stamper.getImportedPage(titleReader, 1);
        stamper.insertPage(1, titleReader.getPageSize(1));
        PdfContentByte content = stamper.getUnderContent(1);
        content.addTemplate(page, 0, 0);
        copyLinks(stamper, 1, titleReader, 1);

        PdfDictionary root = reader.getCatalog();
        PdfDictionary labels = root.getAsDict(PdfName.PAGELABELS);
        if (labels != null)
        {
            PdfArray newNums = new PdfArray();
            
            newNums.add(new PdfNumber(0));
            PdfDictionary coverDict = new PdfDictionary();
            coverDict.put(PdfName.P, new PdfString("Cover Page"));
            newNums.add(coverDict);

            PdfArray nums = labels.getAsArray(PdfName.NUMS);
            if (nums != null)
            {
                for (int i = 0; i < nums.size() - 1; )
                {
                    int n = nums.getAsNumber(i++).intValue();
                    newNums.add(new PdfNumber(n+1));
                    newNums.add(nums.getPdfObject(i++));
                }
            }

            labels.put(PdfName.NUMS, newNums);
            stamper.markUsed(labels);
        }

        stamper.close();
    }
}