pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Maruan Sahyoun <sahy...@fileaffairs.de>
Subject Re: PDStream.toByteArray() always returning zero length array
Date Wed, 17 May 2017 07:35:18 GMT
Hi,

> Am 17.05.2017 um 09:30 schrieb Martin Lynch <M.Lynch@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<mailto:m.lynch@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


Mime
View raw message