You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Gstöttner Siegfried <si...@sbg.ac.at> on 2016/07/27 12:42:37 UTC

Problem with PDFMergerExample()

Hello all!

I am using the original example from
https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/PDFMergerExample.java?view=log
and get an End-of-File-Error at the line
pdfMerger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
in
public InputStream merge(List<InputStream> sources) throws IOException {...}

Snippets of my code are:

List:
    private final List<InputStream> mergedPDF = new ArrayList<InputStream>();
    public List<InputStream> getMergedPDF() {
        return mergedPDF;
    }

Load a pdf:
PDDocument docPer = PDDocument.load(new File("..."));

Adding items to the List in a loop:
getMergedPDF().add(oneDocSerial(docPer));

Compute docPer:
    private InputStream oneDocSerial(PDDocument doc) throws IOException {
        // do something with doc
        PDStream ps = new PDStream(doc);
        InputStream output = ps.createInputStream();
        return output;
    }

Could anybody please tell me what to do...? If possible in a more detailed manner, I'm a beginner...

Kind regards from Salzburg, Austria,
Siegfried
-
Siegfried Gstöttner
siegfried.gstoettner@sbg.ac.at


AW: Problem with PDFMergerExample()

Posted by Gstöttner Siegfried <si...@sbg.ac.at>.
Thank you Tilman!

I will take your advice and get it to work this way.

Herzliche Grüsse,
Siegfried
-
Siegfried Gstöttner
siegfried.gstoettner@sbg.ac.at
Universitätsbibliothek Salzburg, Bibliothekstechnologie
Hofstallgasse 2-4
5020 Salzburg
AUSTRIA


-----Ursprüngliche Nachricht-----
Von: Tilman Hausherr [mailto:THausherr@t-online.de] 
Gesendet: Donnerstag, 28. Juli 2016 18:12
An: users@pdfbox.apache.org
Betreff: Re: Problem with PDFMergerExample()

Am 28.07.2016 um 09:16 schrieb Gstöttner Siegfried:
> Hello Tilman!
>
> Thank you for your quick response. Ist is now working fine. Thank you very much.

Btw what you do is a bit of an overkill for a mailing, although the good thing is that you have something working so you can impress your boss :-) The better way to do it is to create a new PDDocument, then add a page to it (importPage(), not addPage()) and modify each by adding the address.

Another thing to take care of: if you append your own content stream to a page, then use the one with the fifth parameter set to true. This is to take care of PDFs where the content stream does not reset the graphics at the end.

Tilman
>
>
> Kind regards,
> Siegfried
> -
> Siegfried Gstöttner
> siegfried.gstoettner@sbg.ac.at
>
>
> -----Ursprüngliche Nachricht-----
> Von: Tilman Hausherr [mailto:THausherr@t-online.de]
> Gesendet: Donnerstag, 28. Juli 2016 08:01
> An: users@pdfbox.apache.org
> Betreff: Re: Problem with PDFMergerExample()
>
> Am 28.07.2016 um 07:41 schrieb Gstöttner Siegfried:
>> The "doc" in "oneDocSerial" is the main document originally loaded from a file. Then an adress is added to this document. Could you please tell me how to convert the result into avalid InputStream to get your example working?
> Do this:
>
>
> ByteArrayOutputStream baos = new ByteArrayOutputStream();
>
> doc.save(baos);
>
> doc.close(); // possibly much later, depending if doc uses resources from other documents!
>
> ByteArrayInputStream bais = new 
> ByteArrayInputStrea(baos.toByteArray());
>
> bais is now an input stream.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Problem with PDFMergerExample()

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 28.07.2016 um 09:16 schrieb Gstttner Siegfried:
> Hello Tilman!
>
> Thank you for your quick response. Ist is now working fine. Thank you very much.

Btw what you do is a bit of an overkill for a mailing, although the good 
thing is that you have something working so you can impress your boss :-)
The better way to do it is to create a new PDDocument, then add a page 
to it (importPage(), not addPage()) and modify each by adding the address.

Another thing to take care of: if you append your own content stream to 
a page, then use the one with the fifth parameter set to true. This is 
to take care of PDFs where the content stream does not reset the 
graphics at the end.

Tilman
>
>
> Kind regards,
> Siegfried
> -
> Siegfried Gstttner
> siegfried.gstoettner@sbg.ac.at
>
>
> -----Ursprngliche Nachricht-----
> Von: Tilman Hausherr [mailto:THausherr@t-online.de]
> Gesendet: Donnerstag, 28. Juli 2016 08:01
> An: users@pdfbox.apache.org
> Betreff: Re: Problem with PDFMergerExample()
>
> Am 28.07.2016 um 07:41 schrieb Gstttner Siegfried:
>> The "doc" in "oneDocSerial" is the main document originally loaded from a file. Then an adress is added to this document. Could you please tell me how to convert the result into avalid InputStream to get your example working?
> Do this:
>
>
> ByteArrayOutputStream baos = new ByteArrayOutputStream();
>
> doc.save(baos);
>
> doc.close(); // possibly much later, depending if doc uses resources from other documents!
>
> ByteArrayInputStream bais = new ByteArrayInputStrea(baos.toByteArray());
>
> bais is now an input stream.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


AW: Problem with PDFMergerExample()

Posted by Gstöttner Siegfried <si...@sbg.ac.at>.
Hello Tilman!

Thank you for your quick response. Ist is now working fine. Thank you very much.


Kind regards,
Siegfried
-
Siegfried Gstöttner
siegfried.gstoettner@sbg.ac.at


-----Ursprüngliche Nachricht-----
Von: Tilman Hausherr [mailto:THausherr@t-online.de] 
Gesendet: Donnerstag, 28. Juli 2016 08:01
An: users@pdfbox.apache.org
Betreff: Re: Problem with PDFMergerExample()

Am 28.07.2016 um 07:41 schrieb Gstöttner Siegfried:
> The "doc" in "oneDocSerial" is the main document originally loaded from a file. Then an adress is added to this document. Could you please tell me how to convert the result into avalid InputStream to get your example working?

Do this:


ByteArrayOutputStream baos = new ByteArrayOutputStream();

doc.save(baos);

doc.close(); // possibly much later, depending if doc uses resources from other documents!

ByteArrayInputStream bais = new ByteArrayInputStrea(baos.toByteArray());

bais is now an input stream.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Problem with PDFMergerExample()

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 28.07.2016 um 07:41 schrieb Gstttner Siegfried:
> The "doc" in "oneDocSerial" is the main document originally loaded from a file. Then an adress is added to this document. Could you please tell me how to convert the result into avalid InputStream to get your example working?

Do this:


ByteArrayOutputStream baos = new ByteArrayOutputStream();

doc.save(baos);

doc.close(); // possibly much later, depending if doc uses resources 
from other documents!

ByteArrayInputStream bais = new ByteArrayInputStrea(baos.toByteArray());

bais is now an input stream.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


AW: Problem with PDFMergerExample()

Posted by Gstöttner Siegfried <si...@sbg.ac.at>.
Hi!

Thank's for replying Tilman. What i want is to merge several different pdf's to one single document. I have a main document from a file and an adress-database. I take the main document, put an adress on it. Take the same main document, put the next adress on it and so on. This is done at "oneDocSerial". At the end of that loop i want to merge these documents into one single pdf. This single pdf shall now be displayed for the user and maybe printed out. Not saved to file.

The conversion into InputStream is done only for the reason being able to use your Example.

You mentioned this were an empty stream:
Compute docPer:
    private InputStream oneDocSerial(PDDocument doc) throws IOException {
        // do something with doc -> put an adress there
        PDStream ps = new PDStream(doc);
        InputStream output = ps.createInputStream();
        return output;
    }
The "doc" in "oneDocSerial" is the main document originally loaded from a file. Then an adress is added to this document. Could you please tell me how to convert the result into avalid InputStream to get your example working?

Or is there a directer way to merge the adress-pdf documents into one single without need using InputStream?

Kind regards,
Siegfried
-
Siegfried Gstöttner
siegfried.gstoettner@sbg.ac.at

-----Ursprüngliche Nachricht-----
Von: Tilman Hausherr [mailto:THausherr@t-online.de] 
Gesendet: Mittwoch, 27. Juli 2016 19:40
An: users@pdfbox.apache.org
Betreff: Re: Problem with PDFMergerExample()

Am 27.07.2016 um 14:42 schrieb Gstöttner Siegfried:
> Hello all!
>
> I am using the original example from
> https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/
> apache/pdfbox/examples/util/PDFMergerExample.java?view=log
> and get an End-of-File-Error at the line 
> pdfMerger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
> in
> public InputStream merge(List<InputStream> sources) throws IOException 
> {...}
>
> Snippets of my code are:
>
> List:
>      private final List<InputStream> mergedPDF = new ArrayList<InputStream>();
>      public List<InputStream> getMergedPDF() {
>          return mergedPDF;
>      }
>
> Load a pdf:
> PDDocument docPer = PDDocument.load(new File("..."));
>
> Adding items to the List in a loop:
> getMergedPDF().add(oneDocSerial(docPer));
>
> Compute docPer:
>      private InputStream oneDocSerial(PDDocument doc) throws IOException {
>          // do something with doc
>          PDStream ps = new PDStream(doc);
>          InputStream output = ps.createInputStream();
>          return output;
>      }

That is not a PDF. That is an empty stream.

> Could anybody please tell me what to do...? If possible in a more detailed manner, I'm a beginner...

what do you want to do? If you want the input stream of a PDF (which doesn't make sense if you have the PDF as a file), then do this:

PDDocument.load(new FileInputStream(new File(...)))


Tilman


>
> Kind regards from Salzburg, Austria,
> Siegfried
> -
> Siegfried Gstöttner
> siegfried.gstoettner@sbg.ac.at
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Problem with PDFMergerExample()

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 27.07.2016 um 14:42 schrieb Gstttner Siegfried:
> Hello all!
>
> I am using the original example from
> https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/PDFMergerExample.java?view=log
> and get an End-of-File-Error at the line
> pdfMerger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
> in
> public InputStream merge(List<InputStream> sources) throws IOException {...}
>
> Snippets of my code are:
>
> List:
>      private final List<InputStream> mergedPDF = new ArrayList<InputStream>();
>      public List<InputStream> getMergedPDF() {
>          return mergedPDF;
>      }
>
> Load a pdf:
> PDDocument docPer = PDDocument.load(new File("..."));
>
> Adding items to the List in a loop:
> getMergedPDF().add(oneDocSerial(docPer));
>
> Compute docPer:
>      private InputStream oneDocSerial(PDDocument doc) throws IOException {
>          // do something with doc
>          PDStream ps = new PDStream(doc);
>          InputStream output = ps.createInputStream();
>          return output;
>      }

That is not a PDF. That is an empty stream.

> Could anybody please tell me what to do...? If possible in a more detailed manner, I'm a beginner...

what do you want to do? If you want the input stream of a PDF (which 
doesn't make sense if you have the PDF as a file), then do this:

PDDocument.load(new FileInputStream(new File(...)))


Tilman


>
> Kind regards from Salzburg, Austria,
> Siegfried
> -
> Siegfried Gstttner
> siegfried.gstoettner@sbg.ac.at
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


WG: Problem with PDFMergerExample()

Posted by Gstöttner Siegfried <si...@sbg.ac.at>.
Hello all!

I forgot something:
        // Merge all
        PDDocument docMain;
        PDFMergerExample mergerEx = new PDFMergerExample();
        docMain = PDDocument.load(mergerEx.merge(getMergedPDF()));

-----Ursprüngliche Nachricht-----
Von: Gstöttner Siegfried [mailto:siegfried.gstoettner@sbg.ac.at] 
Gesendet: Mittwoch, 27. Juli 2016 14:43
An: users@pdfbox.apache.org
Betreff: Problem with PDFMergerExample()

Hello all!

I am using the original example from
https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/PDFMergerExample.java?view=log
and get an End-of-File-Error at the line pdfMerger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
in
public InputStream merge(List<InputStream> sources) throws IOException {...}

Snippets of my code are:

List:
    private final List<InputStream> mergedPDF = new ArrayList<InputStream>();
    public List<InputStream> getMergedPDF() {
        return mergedPDF;
    }

Load a pdf:
PDDocument docPer = PDDocument.load(new File("..."));

Adding items to the List in a loop:
getMergedPDF().add(oneDocSerial(docPer));

Compute docPer:
    private InputStream oneDocSerial(PDDocument doc) throws IOException {
        // do something with doc
        PDStream ps = new PDStream(doc);
        InputStream output = ps.createInputStream();
        return output;
    }

Could anybody please tell me what to do...? If possible in a more detailed manner, I'm a beginner...

Kind regards from Salzburg, Austria,
Siegfried
-
Siegfried Gstöttner
siegfried.gstoettner@sbg.ac.at


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org