You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by "Vorpahl,Lucinda" <vo...@heb.com> on 2015/02/11 23:11:47 UTC

Overlay PDF Docs

I am hoping someone can help me.

I have a list of PDDocuments that I want to combine and draw onto 1 PDDocument.
I was able to do this by converting the individual documents to images then drawing the image on the new document but I lost image quality.
I was looking at the Overlay option but I'm getting the following error:
	"java.lang.UnsupportedOperationException: Layout pages with COSArray currently not supported."

I also tried this but nothing was happening.

	PDPageContentStream contentStream;
         PDDocument document = new PDDocument();
         PDPage page = new PDPage();
         document.addPage(page);
         AffineTransform transform;
for (PDDocument pdDocument : pdfDocList) {
            LayerUtility layerUtility = new LayerUtility(document);
            PDXObjectForm mountable = layerUtility.importPageAsForm(pdDocument, 0);
            mountable.getPDStream().addCompression();
            contentStream = new PDPageContentStream(document, page);
            contentStream.appendRawCommands("q\n".getBytes("ISO-8859-1"));

            transform = new AffineTransform();
            transform.translate(18.0f, 18.0f);
            contentStream.drawXObject(mountable, transform);

            contentStream.appendRawCommands("Q\n".getBytes("ISO-8859-1"));
            contentStream.close();
	pdDocument.close();
}

Any help would be appreciated.


Thanks,
Lucy

RE: Overlay PDF Docs

Posted by "Vorpahl,Lucinda" <vo...@heb.com>.
What I'm doing is cropping a list of PDDocuments down to small rectangles.

Then I want to combine all the new cropped PDDocuments into one new PDDocument but I want them in specific locations.

Here’s an example of what I’m trying to do.



[cid:image001.png@01D04695.96917190]





I am currently doing something similar but I'm only using the original PDDocument.



private void drawSixUp(PDDocument document, PDPage page) throws IOException {

        final float rectangle_width = 2.25f * PDFx;

        final float rectangle_x_separation = 9.0f / 16.0f * PDFx;



        LayerUtility layerUtility = new LayerUtility(document);

        PDXObjectForm mountable = layerUtility.importPageAsForm(document, 0);

        mountable.getPDStream().addCompression();

        PDPageContentStream contentStream = new PDPageContentStream(document, page, true, true);

        contentStream.appendRawCommands("q\n".getBytes("ISO-8859-1"));



  //draw additional rectangles

        AffineTransform transform = new AffineTransform();

        transform.translate(rectangle_width + rectangle_x_separation, 0);

        contentStream.drawXObject(mountable, transform);



        transform = new AffineTransform();

        transform.translate(rectangle_width * 2 + rectangle_x_separation * 2, 0);

        contentStream.drawXObject(mountable, transform);



        contentStream.appendRawCommands("Q\n".getBytes("ISO-8859-1"));

        contentStream.close();



    }



I’ve tried doing the same thing but for some reason it’s not working.

Here is what I’m trying:
public void drawMealDealStore(List<PDDocument> pdfImagesList) throws IOException {

    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage(page);
    PDPageContentStream contentStream;
    AffineTransform transform;

    float couponGap = 0;
    int i = 1;
    for (PDDocument pdDocument : pdfImagesList) {
        LayerUtility layerUtility = new LayerUtility(document);
        PDXObjectForm mountable = layerUtility.importPageAsForm(pdDocument, 0);
        mountable.getPDStream().addCompression();
        contentStream = new PDPageContentStream(document, page, true, true);
        contentStream.appendRawCommands("q\n".getBytes("ISO-8859-1"));
        boolean topRow;
        if (i != 1 && i % SIX == 1) {
            page = new PDPage();
            document.addPage(page);
            contentStream = newPDPageContentStream(document, page);
            couponGap = 0;
            topRow = true;
        } else if (i == 1) {
            couponGap = 0;
            topRow = true;
        } else if (i % SIX == TWO || i % SIX == THREE) {
            couponGap += COUPONGAP;
            topRow = true;
        } else if (i % SIX == FOUR) {
            couponGap = 0;
            topRow = false;
        } else {
            couponGap += COUPONGAP;
            topRow = false;
        }
        transform = new AffineTransform();

        if (topRow) {
            transform.translate((X_INDENT*2) + couponGap, BOTTM_OF_TOP_REC);
        } else {
            transform.translate((X_INDENT * 2) + couponGap, BOTTM_OF_TOP_REC - (REC_Y_GAP + REC_HEIGHT));
        }
        contentStream.drawXObject(mountable, transform);

        // restore former graphics state
        contentStream.appendRawCommands("Q\n".getBytes("ISO-8859-1"));
        contentStream.close();
        pdDocument.close();
        i++;
    }

    try {
        document.save("mealDealStore.pdf");
    } catch (COSVisitorException e) {
        e.printStackTrace();
    }
    document.close();
}



This should work but all I’m getting is an empty page and the following error:

[cid:image002.png@01D04698.C052A780]



Any ideas of what I’m doing wrong?



Thanks,

Lucy

-----Original Message-----
From: Gilad Denneboom [mailto:gilad.denneboom@gmail.com]
Sent: Thursday, February 12, 2015 5:28 AM
To: users@pdfbox.apache.org
Subject: Re: Overlay PDF Docs



Are you basically trying to combine multiple PDF files into a single one?

If so, use the PDFMergerUtility

<https://urldefense.proofpoint.com/v2/url?u=https-3A__pdfbox.apache.org_apidocs_org_apache_pdfbox_util_PDFMergerUtility.html&d=AwIBaQ&c=RI9dKKMRNVHr9NFa7OQiQw&r=46g1Q9l2ygNJoVl15Feuc2oj-eK5kwz0sL2OtXB9UQA&m=jftZpb6dXxgFhrFo9NoLhae2NQ6p3wKsyO5XQ6E2aS4&s=bmtD8HX65vDYwnzDBlDS_QCzRD6dNpgNnvrvgS3dvNU&e= <https://urldefense.proofpoint.com/v2/url?u=https-3A__pdfbox.apache.org_apidocs_org_apache_pdfbox_util_PDFMergerUtility.html&d=AwIBaQ&c=RI9dKKMRNVHr9NFa7OQiQw&r=46g1Q9l2ygNJoVl15Feuc2oj-eK5kwz0sL2OtXB9UQA&m=jftZpb6dXxgFhrFo9NoLhae2NQ6p3wKsyO5XQ6E2aS4&s=bmtD8HX65vDYwnzDBlDS_QCzRD6dNpgNnvrvgS3dvNU&e=%20> > class.



On Thu, Feb 12, 2015 at 12:11 AM, Vorpahl,Lucinda <vo...@heb.com>>

wrote:



> I am hoping someone can help me.

>

> I have a list of PDDocuments that I want to combine and draw onto 1

> PDDocument.

> I was able to do this by converting the individual documents to images

> then drawing the image on the new document but I lost image quality.

> I was looking at the Overlay option but I'm getting the following error:

>         "java.lang.UnsupportedOperationException: Layout pages with

> COSArray currently not supported."

>

> I also tried this but nothing was happening.

>

>         PDPageContentStream contentStream;

>          PDDocument document = new PDDocument();

>          PDPage page = new PDPage();

>          document.addPage(page);

>          AffineTransform transform;

> for (PDDocument pdDocument : pdfDocList) {

>             LayerUtility layerUtility = new LayerUtility(document);

>             PDXObjectForm mountable =

> layerUtility.importPageAsForm(pdDocument, 0);

>             mountable.getPDStream().addCompression();

>             contentStream = new PDPageContentStream(document, page);

>

> contentStream.appendRawCommands("q\n".getBytes("ISO-8859-1"));

>

>             transform = new AffineTransform();

>             transform.translate(18.0f, 18.0f);

>             contentStream.drawXObject(mountable, transform);

>

>             contentStream.appendRawCommands("Q\n".getBytes("ISO-8859-1"));

>             contentStream.close();

>         pdDocument.close();

> }

>

> Any help would be appreciated.

>

>

> Thanks,

> Lucy

>

Re: Overlay PDF Docs

Posted by Gilad Denneboom <gi...@gmail.com>.
Are you basically trying to combine multiple PDF files into a single one?
If so, use the PDFMergerUtility
<https://pdfbox.apache.org/apidocs/org/apache/pdfbox/util/PDFMergerUtility.html>
class.

On Thu, Feb 12, 2015 at 12:11 AM, Vorpahl,Lucinda <vo...@heb.com>
wrote:

> I am hoping someone can help me.
>
> I have a list of PDDocuments that I want to combine and draw onto 1
> PDDocument.
> I was able to do this by converting the individual documents to images
> then drawing the image on the new document but I lost image quality.
> I was looking at the Overlay option but I'm getting the following error:
>         "java.lang.UnsupportedOperationException: Layout pages with
> COSArray currently not supported."
>
> I also tried this but nothing was happening.
>
>         PDPageContentStream contentStream;
>          PDDocument document = new PDDocument();
>          PDPage page = new PDPage();
>          document.addPage(page);
>          AffineTransform transform;
> for (PDDocument pdDocument : pdfDocList) {
>             LayerUtility layerUtility = new LayerUtility(document);
>             PDXObjectForm mountable =
> layerUtility.importPageAsForm(pdDocument, 0);
>             mountable.getPDStream().addCompression();
>             contentStream = new PDPageContentStream(document, page);
>             contentStream.appendRawCommands("q\n".getBytes("ISO-8859-1"));
>
>             transform = new AffineTransform();
>             transform.translate(18.0f, 18.0f);
>             contentStream.drawXObject(mountable, transform);
>
>             contentStream.appendRawCommands("Q\n".getBytes("ISO-8859-1"));
>             contentStream.close();
>         pdDocument.close();
> }
>
> Any help would be appreciated.
>
>
> Thanks,
> Lucy
>

Re: Overlay PDF Docs

Posted by John Hewson <jo...@jahewson.com>.
Hi Lucy,

Overlay is for adding something like a watermark to each page in an existing document.
To draw existing PDF pages onto a new page, follow our SuperimposePage example.

-- John

> On 11 Feb 2015, at 14:11, Vorpahl,Lucinda <vo...@heb.com> wrote:
> 
> I am hoping someone can help me.
> 
> I have a list of PDDocuments that I want to combine and draw onto 1 PDDocument.
> I was able to do this by converting the individual documents to images then drawing the image on the new document but I lost image quality.
> I was looking at the Overlay option but I'm getting the following error:
> 	"java.lang.UnsupportedOperationException: Layout pages with COSArray currently not supported."
> 
> I also tried this but nothing was happening.
> 
> 	PDPageContentStream contentStream;
>         PDDocument document = new PDDocument();
>         PDPage page = new PDPage();
>         document.addPage(page);
>         AffineTransform transform;
> for (PDDocument pdDocument : pdfDocList) {
>            LayerUtility layerUtility = new LayerUtility(document);
>            PDXObjectForm mountable = layerUtility.importPageAsForm(pdDocument, 0);
>            mountable.getPDStream().addCompression();
>            contentStream = new PDPageContentStream(document, page);
>            contentStream.appendRawCommands("q\n".getBytes("ISO-8859-1"));
> 
>            transform = new AffineTransform();
>            transform.translate(18.0f, 18.0f);
>            contentStream.drawXObject(mountable, transform);
> 
>            contentStream.appendRawCommands("Q\n".getBytes("ISO-8859-1"));
>            contentStream.close();
> 	pdDocument.close();
> }
> 
> Any help would be appreciated.
> 
> 
> Thanks,
> Lucy
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>