You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Martin Lynch <M....@kainos.com> on 2017/05/17 07:30:02 UTC

PDStream.toByteArray() always returning zero length array

Hi

I am using PDFBox 2.0.3

I need to return the byte array representation of a PDDocument without saving it to disk, but I always get a zero length byte array.

My method loads a file and creates a PDDocument from it. It the modifies this document by adding in extra pages and finally I want to return the byte array of the modified document.

If I add report.save(new Fille("Some Path")) then the modified document is correctly saved (but I don't want to do this) - how do I get a valid byte array returned?

Thanks for your help
Martin

Here is my code

URI origReport = ClassLoader.getSystemResource("pdf/FullInvestmentReport.pdf").toURI();

byte[] origReportAsBytes = Files.readAllBytes(Paths.get(origReport));

PDDocument report = PDDocument.load(origReportAsBytes);



URI location1 = ClassLoader.getSystemResource("pdf/AddFile1.pdf").toURI();

byte[] data1 = Files.readAllBytes(Paths.get(location1));

PDDocument docToAdd = PDDocument.load(data1);



int currentPage = 3;

if (docToAdd != null) {
    for (int i = 0; i < docToAdd.getNumberOfPages(); i++) {
        originalReport.getPages().insertAfter(docToAdd.getPage(i), report.getPage(newCurrentPage++));
    }
}



byte[] aggregatedReport = new PDStream(report).toByteArray();



return aggregatedReport;



Martin Lynch | Senior Software Engineer | Kainos | DD: +44 (0)28 9057 1351 | Fax: +44 (0)28 9057 1101 | m.lynch@kainos.com<ma...@kainos.com>


[Sunday Times]<https://www.kainos.com/kainos-cements-position-sunday-times-top-100-best-companies-work/>

Follow the Kainos buzz on: Twitter<https://twitter.com/kainossoftware> Facebook<https://www.facebook.com/KainosSoftware> Linkedin<http://www.linkedin.com/company/kainos> Youtube<http://www.youtube.com/user/KainosSoftware> Kainos cements position sunday times top 100 best companies work<https://www.kainos.com/kainos-cements-position-sunday-times-top-100-best-companies-work/>

This e-mail is for the intended addressee only and is strictly confidential; if you receive it in error please destroy the message and all copies. Any opinion or information in this email or its attachments that does not relate to Kainos business is personal to the sender and is not endorsed by Kainos. This email has been scanned for viruses but is not guaranteed to be virus free. "Kainos" is the trading name of the Kainos Group of companies; click the link for further information https://www.kainos.com/corporate-information/. Further terms and conditions may be found on our website www.kainos.com

Re: PDStream.toByteArray() always returning zero length array

Posted by Maruan Sahyoun <sa...@fileaffairs.de>.
Hi,

> Am 17.05.2017 um 09:30 schrieb Martin Lynch <M....@kainos.com>:
> 
> Hi
> 
> I am using PDFBox 2.0.3
> 
> I need to return the byte array representation of a PDDocument without saving it to disk, but I always get a zero length byte array.
> 
> My method loads a file and creates a PDDocument from it. It the modifies this document by adding in extra pages and finally I want to return the byte array of the modified document.
> 
> If I add report.save(new Fille("Some Path")) then the modified document is correctly saved (but I don't want to do this) - how do I get a valid byte array returned?
> 
> Thanks for your help
> Martin
> 
> Here is my code
> 
> URI origReport = ClassLoader.getSystemResource("pdf/FullInvestmentReport.pdf").toURI();
> 
> byte[] origReportAsBytes = Files.readAllBytes(Paths.get(origReport));
> 
> PDDocument report = PDDocument.load(origReportAsBytes);
> 
> 
> 
> URI location1 = ClassLoader.getSystemResource("pdf/AddFile1.pdf").toURI();
> 
> byte[] data1 = Files.readAllBytes(Paths.get(location1));
> 
> PDDocument docToAdd = PDDocument.load(data1);
> 
> 
> 
> int currentPage = 3;
> 
> if (docToAdd != null) {
>    for (int i = 0; i < docToAdd.getNumberOfPages(); i++) {
>        originalReport.getPages().insertAfter(docToAdd.getPage(i), report.getPage(newCurrentPage++));
>    }
> }
> 
> 
> 
> byte[] aggregatedReport = new PDStream(report).toByteArray();

that's creating a new stream inside the PDF which is a PDF object. That's not the PDF document you are looking for.

Write the PDF to a ByteArrayOutputStream like so

ByteArrayOutputStream output = new ByteArrayOutputStream();
PDDocument,save(output)
byte[] aggregatedReport = output.toByteArray();

BR
Maruan


> 
> 
> 
> return aggregatedReport;
> 
> 
> 
> Martin Lynch | Senior Software Engineer | Kainos | DD: +44 (0)28 9057 1351 | Fax: +44 (0)28 9057 1101 | m.lynch@kainos.com<ma...@kainos.com>
> 
> 
> [Sunday Times]<https://www.kainos.com/kainos-cements-position-sunday-times-top-100-best-companies-work/>
> 
> Follow the Kainos buzz on: Twitter<https://twitter.com/kainossoftware> Facebook<https://www.facebook.com/KainosSoftware> Linkedin<http://www.linkedin.com/company/kainos> Youtube<http://www.youtube.com/user/KainosSoftware> Kainos cements position sunday times top 100 best companies work<https://www.kainos.com/kainos-cements-position-sunday-times-top-100-best-companies-work/>
> 
> This e-mail is for the intended addressee only and is strictly confidential; if you receive it in error please destroy the message and all copies. Any opinion or information in this email or its attachments that does not relate to Kainos business is personal to the sender and is not endorsed by Kainos. This email has been scanned for viruses but is not guaranteed to be virus free. "Kainos" is the trading name of the Kainos Group of companies; click the link for further information https://www.kainos.com/corporate-information/. Further terms and conditions may be found on our website www.kainos.com


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