You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Andy Czerwonka <an...@gmail.com> on 2022/03/20 18:26:08 UTC

Corrupt PDF

I pull the latest 2.x version and am just playing around with the API. I
saw some examples, and was trying to add some text to each page.

The following code creates a corrupt PDF. Wondering if anyone can help me
understand why.

def addUser(user: UserData, is: InputStream): Try[PDDocument] = Try {
val doc = PDDocument.load(is)
if (doc.isEncrypted) {
throw new RuntimeException("We can't adjust the PDF if it's encrypted")
}

val licensee = s"${user.name} (${user.email})"
doc.getPages.forEach { page =>
val contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND,
true, true)
contentStream.beginText()
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 15.0f)
contentStream.setNonStrokingColor(Color.red)
contentStream.setTextMatrix(Matrix.getTranslateInstance(0, 0));
contentStream.showText(licensee);
contentStream.endText();
}

doc
}

Best,

Andy

Re: Corrupt PDF

Posted by Andy Czerwonka <an...@gmail.com>.
Ah.. or course! Silly me... that was it. Thank you!

On Sun, Mar 20, 2022 at 1:00 PM Tilman Hausherr <TH...@t-online.de>
wrote:

> Am 20.03.2022 um 19:48 schrieb Andy Czerwonka:
> > The language that I'm using is Scala, targeting Java 11.
> >
> > I just transposed and simplified this example
> > <
> https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddMessageToEachPage.java?view=markup>,
>
> > which doesn't expressly close the content stream.
>
> It does close the content stream, this is the try-with-resources syntax.
> That's why I was asking, because I don't know if scala has a similar
> syntax. You MUST close the content stream.
>
> Binary attachments are blocked, please upload to a sharehoster. And take
> care not to save to the file you were loading from.
>
> Tilman
>
> >
> > I've attached the corrupt file, which I just generated via
> > PDDocument.save.
> >
> >
> > On Sun, Mar 20, 2022 at 12:36 PM Tilman Hausherr
> > <TH...@t-online.de> wrote:
> >
> >     How is it "corrupt"?
> >
> >     What is that programming language and does the "}" close the
> >     content stream?
> >
> >     Tilman
> >
> >     Am 20.03.2022 um 19:26 schrieb Andy Czerwonka:
> >     > I pull the latest 2.x version and am just playing around with
> >     the API. I
> >     > saw some examples, and was trying to add some text to each page.
> >     >
> >     > The following code creates a corrupt PDF. Wondering if anyone
> >     can help me
> >     > understand why.
> >     >
> >     > def addUser(user: UserData, is: InputStream): Try[PDDocument] =
> >     Try {
> >     > val doc = PDDocument.load(is)
> >     > if (doc.isEncrypted) {
> >     > throw new RuntimeException("We can't adjust the PDF if it's
> >     encrypted")
> >     > }
> >     >
> >     > val licensee = s"${user.name <http://user.name>} (${user.email})"
> >     > doc.getPages.forEach { page =>
> >     > val contentStream = new PDPageContentStream(doc, page,
> >     AppendMode.APPEND,
> >     > true, true)
> >     > contentStream.beginText()
> >     > contentStream.setFont(PDType1Font.HELVETICA_BOLD, 15.0f)
> >     > contentStream.setNonStrokingColor(Color.red)
> >     > contentStream.setTextMatrix(Matrix.getTranslateInstance(0, 0));
> >     > contentStream.showText(licensee);
> >     > contentStream.endText();
> >     > }
> >     >
> >     > doc
> >     > }
> >     >
> >     > Best,
> >     >
> >     > Andy
> >     >
> >
> >
> >     ---------------------------------------------------------------------
> >     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: Corrupt PDF

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 20.03.2022 um 19:48 schrieb Andy Czerwonka:
> The language that I'm using is Scala, targeting Java 11.
>
> I just transposed and simplified this example 
> <https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddMessageToEachPage.java?view=markup>, 
> which doesn't expressly close the content stream.

It does close the content stream, this is the try-with-resources syntax. 
That's why I was asking, because I don't know if scala has a similar 
syntax. You MUST close the content stream.

Binary attachments are blocked, please upload to a sharehoster. And take 
care not to save to the file you were loading from.

Tilman

>
> I've attached the corrupt file, which I just generated via 
> PDDocument.save.
>
>
> On Sun, Mar 20, 2022 at 12:36 PM Tilman Hausherr 
> <TH...@t-online.de> wrote:
>
>     How is it "corrupt"?
>
>     What is that programming language and does the "}" close the
>     content stream?
>
>     Tilman
>
>     Am 20.03.2022 um 19:26 schrieb Andy Czerwonka:
>     > I pull the latest 2.x version and am just playing around with
>     the API. I
>     > saw some examples, and was trying to add some text to each page.
>     >
>     > The following code creates a corrupt PDF. Wondering if anyone
>     can help me
>     > understand why.
>     >
>     > def addUser(user: UserData, is: InputStream): Try[PDDocument] =
>     Try {
>     > val doc = PDDocument.load(is)
>     > if (doc.isEncrypted) {
>     > throw new RuntimeException("We can't adjust the PDF if it's
>     encrypted")
>     > }
>     >
>     > val licensee = s"${user.name <http://user.name>} (${user.email})"
>     > doc.getPages.forEach { page =>
>     > val contentStream = new PDPageContentStream(doc, page,
>     AppendMode.APPEND,
>     > true, true)
>     > contentStream.beginText()
>     > contentStream.setFont(PDType1Font.HELVETICA_BOLD, 15.0f)
>     > contentStream.setNonStrokingColor(Color.red)
>     > contentStream.setTextMatrix(Matrix.getTranslateInstance(0, 0));
>     > contentStream.showText(licensee);
>     > contentStream.endText();
>     > }
>     >
>     > doc
>     > }
>     >
>     > Best,
>     >
>     > Andy
>     >
>
>
>     ---------------------------------------------------------------------
>     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: Corrupt PDF

Posted by Andy Czerwonka <an...@gmail.com>.
The language that I'm using is Scala, targeting Java 11.

I just transposed and simplified this example
<https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddMessageToEachPage.java?view=markup>,
which doesn't expressly close the content stream.

I've attached the corrupt file, which I just generated via PDDocument.save.


On Sun, Mar 20, 2022 at 12:36 PM Tilman Hausherr <TH...@t-online.de>
wrote:

> How is it "corrupt"?
>
> What is that programming language and does the "}" close the content
> stream?
>
> Tilman
>
> Am 20.03.2022 um 19:26 schrieb Andy Czerwonka:
> > I pull the latest 2.x version and am just playing around with the API. I
> > saw some examples, and was trying to add some text to each page.
> >
> > The following code creates a corrupt PDF. Wondering if anyone can help me
> > understand why.
> >
> > def addUser(user: UserData, is: InputStream): Try[PDDocument] = Try {
> > val doc = PDDocument.load(is)
> > if (doc.isEncrypted) {
> > throw new RuntimeException("We can't adjust the PDF if it's encrypted")
> > }
> >
> > val licensee = s"${user.name} (${user.email})"
> > doc.getPages.forEach { page =>
> > val contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND,
> > true, true)
> > contentStream.beginText()
> > contentStream.setFont(PDType1Font.HELVETICA_BOLD, 15.0f)
> > contentStream.setNonStrokingColor(Color.red)
> > contentStream.setTextMatrix(Matrix.getTranslateInstance(0, 0));
> > contentStream.showText(licensee);
> > contentStream.endText();
> > }
> >
> > doc
> > }
> >
> > Best,
> >
> > Andy
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>

Re: Corrupt PDF

Posted by Tilman Hausherr <TH...@t-online.de>.
How is it "corrupt"?

What is that programming language and does the "}" close the content stream?

Tilman

Am 20.03.2022 um 19:26 schrieb Andy Czerwonka:
> I pull the latest 2.x version and am just playing around with the API. I
> saw some examples, and was trying to add some text to each page.
>
> The following code creates a corrupt PDF. Wondering if anyone can help me
> understand why.
>
> def addUser(user: UserData, is: InputStream): Try[PDDocument] = Try {
> val doc = PDDocument.load(is)
> if (doc.isEncrypted) {
> throw new RuntimeException("We can't adjust the PDF if it's encrypted")
> }
>
> val licensee = s"${user.name} (${user.email})"
> doc.getPages.forEach { page =>
> val contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND,
> true, true)
> contentStream.beginText()
> contentStream.setFont(PDType1Font.HELVETICA_BOLD, 15.0f)
> contentStream.setNonStrokingColor(Color.red)
> contentStream.setTextMatrix(Matrix.getTranslateInstance(0, 0));
> contentStream.showText(licensee);
> contentStream.endText();
> }
>
> doc
> }
>
> Best,
>
> Andy
>


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