You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by A Brand <a...@awews.com> on 2018/05/16 22:44:49 UTC

Add Text To A PDF Document

I'd like to add text to an existing pdf document. I attempted it and the
current page was erased and replaced with the text I wanted to add.
I tried using the Overlay() and didn't see the text added.

Is this a feature of pdfbox?

Onward.
Don't Be Good. Be Great. Be Grateful.

Please note this message was edited for brevity and clarity.

Best regards,
Anthony McDonald


*602-456-9335*
http://www.idnkhtw.space

Re: Add Text To A PDF Document

Posted by a...@awews.com, a...@awews.com.
I'm using version 2.0.4 .
I didn't see the options. Where do I put the 'append' option?

On 2018/05/17 03:41:35, Tilman Hausherr <TH...@t-online.de> wrote: 
> Am 17.05.2018 um 00:44 schrieb A Brand:
> > I'd like to add text to an existing pdf document. I attempted it and the
> > current page was erased and replaced with the text I wanted to add.
> 
> You probably used the overwrite option or no option instead of using the 
> append option when creating your PDPageContentStream.
> 
> If it still doesn't work, please show your code and mention the version 
> you are using (hopefully 2.0.9).
> 
> > I tried using the Overlay() and didn't see the text added.
> >
> > Is this a feature of pdfbox?
> 
> Yes it is!
> 
> Tilman
> 
> 
> >
> > Onward.
> > Don't Be Good. Be Great. Be Grateful.
> >
> > Please note this message was edited for brevity and clarity.
> >
> > Best regards,
> > Anthony McDonald
> >
> >
> > *602-456-9335*
> > http://www.idnkhtw.space
> >
> 
> 
> ---------------------------------------------------------------------
> 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: Add Text To A PDF Document

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 17.05.2018 um 00:44 schrieb A Brand:
> I'd like to add text to an existing pdf document. I attempted it and the
> current page was erased and replaced with the text I wanted to add.

You probably used the overwrite option or no option instead of using the 
append option when creating your PDPageContentStream.

If it still doesn't work, please show your code and mention the version 
you are using (hopefully 2.0.9).

> I tried using the Overlay() and didn't see the text added.
>
> Is this a feature of pdfbox?

Yes it is!

Tilman


>
> Onward.
> Don't Be Good. Be Great. Be Grateful.
>
> Please note this message was edited for brevity and clarity.
>
> Best regards,
> Anthony McDonald
>
>
> *602-456-9335*
> http://www.idnkhtw.space
>


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


Re: Add Text To A PDF Document

Posted by Tilman Hausherr <TH...@t-online.de>.
PDPageContentStream.AppendMode.APPEND
PDPageContentStream.AppendMode.OVERWRITE
PDPageContentStream.AppendMode.PREPEND

You can also use

AppendMode.APPEND
AppendMode.OVERWRITE
AppendMode.PREPEND

and press "fix imports" shortcut in your IDE (in Netbeans, it is 
CTRL-Shift-i )

In your IDE, there's usually a shortcut to expand stuff. In Netbeans it 
is CTRL-space.

Tilman

Am 26.05.2018 um 02:21 schrieb curb.appeall@gmail.com:
> I'm having a similar issue.
> How do you use the AppendMode appendContent parameter?
>
> On 2018/05/22 17:55:46, Tilman Hausherr <TH...@t-online.de> wrote:
>> Am 22.05.2018 um 19:47 schrieb A Brand:
>>> I'm using version 2.0.4 .
>> Current version is 2.0.9.
>>
>>
>>> I didn't see the options. Where do I put the 'append' option?
>> it's the third parameter in one of the PDPageContentStream constructors.
>> And set the very last parameter to true.
>>
>>
>>       /**
>>        * Create a new PDPage content stream.
>>        *
>>        * @param document The document the page is part of.
>>        * @param sourcePage The page to write the contents to.
>>        * @param appendContent Indicates whether content will be
>> overwritten, appended or prepended.
>>        * @param compress Tell if the content stream should compress the
>> page contents.
>>        * @param resetContext Tell if the graphic context should be reset.
>> This is only relevant when
>>        * the appendContent parameter is set to {@link AppendMode#APPEND}.
>> You should use this when
>>        * appending to an existing stream, because the existing stream may
>> have changed graphic
>>        * properties (e.g. scaling, rotation).
>>        * @throws IOException If there is an error writing to the page
>> contents.
>>        */
>>       public PDPageContentStream(PDDocument document, PDPage sourcePage,
>> AppendMode appendContent,
>>                                  boolean compress, boolean resetContext)
>> throws IOException
>>
>>
>>
>>
>> Tilman
>>
>>
>>> Onward.
>>> Don't Be Good. Be Great. Be Grateful.
>>>
>>> Please note this message was edited for brevity and clarity.
>>>
>>> Best regards,
>>> Anthony McDonald
>>>
>>>
>>> *602-456-9335*
>>> http://www.idnkhtw.space
>>>
>>> On Wed, May 16, 2018 at 6:44 PM, A Brand <a...@awews.com> wrote:
>>>
>>>> I'd like to add text to an existing pdf document. I attempted it and the
>>>> current page was erased and replaced with the text I wanted to add.
>>>> I tried using the Overlay() and didn't see the text added.
>>>>
>>>> Is this a feature of pdfbox?
>>>>
>>>> Onward.
>>>> Don't Be Good. Be Great. Be Grateful.
>>>>
>>>> Please note this message was edited for brevity and clarity.
>>>>
>>>> Best regards,
>>>> Anthony McDonald
>>>>
>>>>
>>>> *602-456-9335*
>>>> http://www.idnkhtw.space
>>>>
>>
>> ---------------------------------------------------------------------
>> 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: Add Text To A PDF Document

Posted by cu...@gmail.com, cu...@gmail.com.
I'm having a similar issue.
How do you use the AppendMode appendContent parameter?

On 2018/05/22 17:55:46, Tilman Hausherr <TH...@t-online.de> wrote: 
> Am 22.05.2018 um 19:47 schrieb A Brand:
> > I'm using version 2.0.4 .
> 
> Current version is 2.0.9.
> 
> 
> > I didn't see the options. Where do I put the 'append' option?
> 
> it's the third parameter in one of the PDPageContentStream constructors. 
> And set the very last parameter to true.
> 
> 
>      /**
>       * Create a new PDPage content stream.
>       *
>       * @param document The document the page is part of.
>       * @param sourcePage The page to write the contents to.
>       * @param appendContent Indicates whether content will be 
> overwritten, appended or prepended.
>       * @param compress Tell if the content stream should compress the 
> page contents.
>       * @param resetContext Tell if the graphic context should be reset. 
> This is only relevant when
>       * the appendContent parameter is set to {@link AppendMode#APPEND}. 
> You should use this when
>       * appending to an existing stream, because the existing stream may 
> have changed graphic
>       * properties (e.g. scaling, rotation).
>       * @throws IOException If there is an error writing to the page 
> contents.
>       */
>      public PDPageContentStream(PDDocument document, PDPage sourcePage, 
> AppendMode appendContent,
>                                 boolean compress, boolean resetContext) 
> throws IOException
> 
> 
> 
> 
> Tilman
> 
> 
> >
> > Onward.
> > Don't Be Good. Be Great. Be Grateful.
> >
> > Please note this message was edited for brevity and clarity.
> >
> > Best regards,
> > Anthony McDonald
> >
> >
> > *602-456-9335*
> > http://www.idnkhtw.space
> >
> > On Wed, May 16, 2018 at 6:44 PM, A Brand <a...@awews.com> wrote:
> >
> >> I'd like to add text to an existing pdf document. I attempted it and the
> >> current page was erased and replaced with the text I wanted to add.
> >> I tried using the Overlay() and didn't see the text added.
> >>
> >> Is this a feature of pdfbox?
> >>
> >> Onward.
> >> Don't Be Good. Be Great. Be Grateful.
> >>
> >> Please note this message was edited for brevity and clarity.
> >>
> >> Best regards,
> >> Anthony McDonald
> >>
> >>
> >> *602-456-9335*
> >> http://www.idnkhtw.space
> >>
> 
> 
> ---------------------------------------------------------------------
> 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: Add Text To A PDF Document

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 22.05.2018 um 19:47 schrieb A Brand:
> I'm using version 2.0.4 .

Current version is 2.0.9.


> I didn't see the options. Where do I put the 'append' option?

it's the third parameter in one of the PDPageContentStream constructors. 
And set the very last parameter to true.


     /**
      * Create a new PDPage content stream.
      *
      * @param document The document the page is part of.
      * @param sourcePage The page to write the contents to.
      * @param appendContent Indicates whether content will be 
overwritten, appended or prepended.
      * @param compress Tell if the content stream should compress the 
page contents.
      * @param resetContext Tell if the graphic context should be reset. 
This is only relevant when
      * the appendContent parameter is set to {@link AppendMode#APPEND}. 
You should use this when
      * appending to an existing stream, because the existing stream may 
have changed graphic
      * properties (e.g. scaling, rotation).
      * @throws IOException If there is an error writing to the page 
contents.
      */
     public PDPageContentStream(PDDocument document, PDPage sourcePage, 
AppendMode appendContent,
                                boolean compress, boolean resetContext) 
throws IOException




Tilman


>
> Onward.
> Don't Be Good. Be Great. Be Grateful.
>
> Please note this message was edited for brevity and clarity.
>
> Best regards,
> Anthony McDonald
>
>
> *602-456-9335*
> http://www.idnkhtw.space
>
> On Wed, May 16, 2018 at 6:44 PM, A Brand <a...@awews.com> wrote:
>
>> I'd like to add text to an existing pdf document. I attempted it and the
>> current page was erased and replaced with the text I wanted to add.
>> I tried using the Overlay() and didn't see the text added.
>>
>> Is this a feature of pdfbox?
>>
>> Onward.
>> Don't Be Good. Be Great. Be Grateful.
>>
>> Please note this message was edited for brevity and clarity.
>>
>> Best regards,
>> Anthony McDonald
>>
>>
>> *602-456-9335*
>> http://www.idnkhtw.space
>>


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


Re: Add Text To A PDF Document

Posted by A Brand <a...@awews.com>.
I'm using version 2.0.4 .
I didn't see the options. Where do I put the 'append' option?

Onward.
Don't Be Good. Be Great. Be Grateful.

Please note this message was edited for brevity and clarity.

Best regards,
Anthony McDonald


*602-456-9335*
http://www.idnkhtw.space

On Wed, May 16, 2018 at 6:44 PM, A Brand <a...@awews.com> wrote:

> I'd like to add text to an existing pdf document. I attempted it and the
> current page was erased and replaced with the text I wanted to add.
> I tried using the Overlay() and didn't see the text added.
>
> Is this a feature of pdfbox?
>
> Onward.
> Don't Be Good. Be Great. Be Grateful.
>
> Please note this message was edited for brevity and clarity.
>
> Best regards,
> Anthony McDonald
>
>
> *602-456-9335*
> http://www.idnkhtw.space
>

Re: Add Text To A PDF Document

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 26.05.2018 um 02:27 schrieb a@awews.com:
> Is this the correct way to use it?
>
> `               PDDocument document = null;
> 		PDDocument doc = document .load(file);

No, use PDDocument.load(). That is a static call.

> 		PDPage page = doc.getPage(0);
> 		PDPageContentStream contentStream = new PDPageContentStream(doc, page,
>                  PDPageContentStream.AppendMode.APPEND, true, true);

Yes

Tilman

> 		
>
> 		doc.setAllSecurityToBeRemoved(true);
> `
>
> On 2018/05/24 16:15:00, Tilman Hausherr <TH...@t-online.de> wrote:
>> Am 24.05.2018 um 17:11 schrieb a@awews.com:
>>> I was able to get it to work. I just have to fine tune where the text goes. Here's my solution for anyone needing help in the future.
>>> Thank you for your help!
>>> If you have a moment, please let me know if there's a more efficient way to do it.
>> 1) You're using a deprecated method instead of the one I mentioned. This
>> will work fine, but some day the method will be removed
>> 2) no need to do a try-catch around every line.
>>
>> Tilman
>>
>>
>>> `@SpringBootApplication
>>> public class Pdfbox1Application {
>>>
>>> 	public static void main(String[] args) throws IOException {
>>> 		SpringApplication.run(Pdfbox1Application.class, args);
>>> 		
>>> 		File file = new File("./i-90.pdf");
>>> 		
>>> 		PDDocument document = null;
>>> 		PDDocument doc = document .load(file);
>>> 		PDPage page = doc.getPage(1);
>>> 		PDPageContentStream.AppendMode appendContent;
>>> 		PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true);
>>> 				
>>> 		doc.setAllSecurityToBeRemoved(true);
>>> 		
>>> 		
>>> 		
>>> 		try {
>>> 			contentStream.beginText();
>>> 		} catch (IOException e) {
>>> 			// TODO Auto-generated catch block
>>> 			e.printStackTrace();
>>> 		}
>>> 		
>>> 		try {
>>> 			contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);
>>> 			contentStream.newLineAtOffset(25, 700);
>>> 		} catch (IOException e) {
>>> 			// TODO Auto-generated catch block
>>> 			e.printStackTrace();
>>> 		}
>>> 		
>>> 		String text = "This is sample text";
>>> 		
>>> 		
>>> 		
>>> //		try {
>>> //			contentStream = new PDPageContentStream(doc, page);
>>> //		} catch (IOException e) {
>>> //			// TODO Auto-generated catch block
>>> //			e.printStackTrace();
>>> //		}
>>> 		
>>> 		
>>> 		
>>> 		
>>> 		
>>> 		try {
>>> 			contentStream.showText(text);
>>> 		} catch (IOException e) {
>>> 			// TODO Auto-generated catch block
>>> 			e.printStackTrace();
>>> 		}
>>> 		
>>> 		try {
>>> 			contentStream.endText();
>>> 		} catch (IOException e) {
>>> 			// TODO Auto-generated catch block
>>> 			e.printStackTrace();
>>> 		}
>>> 		
>>> 		System.out.println("content added");
>>> 		
>>> 		try {
>>> 			contentStream.close();
>>> 		} catch (IOException e) {
>>> 			// TODO Auto-generated catch block
>>> 			e.printStackTrace();
>>> 		}
>>> 		
>>> 		try {
>>> 			doc.save("./newi-90.pdf");
>>> 		} catch (IOException e) {
>>> 			// TODO Auto-generated catch block
>>> 			e.printStackTrace();
>>> 		}
>>> 		
>>> 		
>>> 		
>>> 		try {
>>> 			doc.close();
>>> 		} catch (IOException e) {
>>> 			// TODO Auto-generated catch block
>>> 			e.printStackTrace();
>>> 		}
>>> 	}
>>> }
>>> `
>>>
>>> On 2018/05/16 22:44:49, A Brand <a...@awews.com> wrote:
>>>> I'd like to add text to an existing pdf document. I attempted it and the
>>>> current page was erased and replaced with the text I wanted to add.
>>>> I tried using the Overlay() and didn't see the text added.
>>>>
>>>> Is this a feature of pdfbox?
>>>>
>>>> Onward.
>>>> Don't Be Good. Be Great. Be Grateful.
>>>>
>>>> Please note this message was edited for brevity and clarity.
>>>>
>>>> Best regards,
>>>> Anthony McDonald
>>>>
>>>>
>>>> *602-456-9335*
>>>> http://www.idnkhtw.space
>>>>
>>> ---------------------------------------------------------------------
>>> 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: Add Text To A PDF Document

Posted by a...@awews.com, a...@awews.com.
Is this the correct way to use it?

`               PDDocument document = null;
		PDDocument doc = document .load(file);
		PDPage page = doc.getPage(0);
		PDPageContentStream contentStream = new PDPageContentStream(doc, page, 
                PDPageContentStream.AppendMode.APPEND, true, true);
		

		doc.setAllSecurityToBeRemoved(true);
`

On 2018/05/24 16:15:00, Tilman Hausherr <TH...@t-online.de> wrote: 
> Am 24.05.2018 um 17:11 schrieb a@awews.com:
> > I was able to get it to work. I just have to fine tune where the text goes. Here's my solution for anyone needing help in the future.
> > Thank you for your help!
> > If you have a moment, please let me know if there's a more efficient way to do it.
> 
> 1) You're using a deprecated method instead of the one I mentioned. This 
> will work fine, but some day the method will be removed
> 2) no need to do a try-catch around every line.
> 
> Tilman
> 
> 
> >
> > `@SpringBootApplication
> > public class Pdfbox1Application {
> >
> > 	public static void main(String[] args) throws IOException {
> > 		SpringApplication.run(Pdfbox1Application.class, args);
> > 		
> > 		File file = new File("./i-90.pdf");
> > 		
> > 		PDDocument document = null;
> > 		PDDocument doc = document .load(file);
> > 		PDPage page = doc.getPage(1);
> > 		PDPageContentStream.AppendMode appendContent;
> > 		PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true);
> > 				
> > 		doc.setAllSecurityToBeRemoved(true);
> > 		
> > 		
> > 		
> > 		try {
> > 			contentStream.beginText();
> > 		} catch (IOException e) {
> > 			// TODO Auto-generated catch block
> > 			e.printStackTrace();
> > 		}
> > 		
> > 		try {
> > 			contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);
> > 			contentStream.newLineAtOffset(25, 700);
> > 		} catch (IOException e) {
> > 			// TODO Auto-generated catch block
> > 			e.printStackTrace();
> > 		}
> > 		
> > 		String text = "This is sample text";
> > 		
> > 		
> > 		
> > //		try {
> > //			contentStream = new PDPageContentStream(doc, page);
> > //		} catch (IOException e) {
> > //			// TODO Auto-generated catch block
> > //			e.printStackTrace();
> > //		}
> > 		
> > 		
> > 		
> > 		
> > 		
> > 		try {
> > 			contentStream.showText(text);
> > 		} catch (IOException e) {
> > 			// TODO Auto-generated catch block
> > 			e.printStackTrace();
> > 		}
> > 		
> > 		try {
> > 			contentStream.endText();
> > 		} catch (IOException e) {
> > 			// TODO Auto-generated catch block
> > 			e.printStackTrace();
> > 		}
> > 		
> > 		System.out.println("content added");
> > 		
> > 		try {
> > 			contentStream.close();
> > 		} catch (IOException e) {
> > 			// TODO Auto-generated catch block
> > 			e.printStackTrace();
> > 		}
> > 		
> > 		try {
> > 			doc.save("./newi-90.pdf");
> > 		} catch (IOException e) {
> > 			// TODO Auto-generated catch block
> > 			e.printStackTrace();
> > 		}
> > 		
> > 		
> > 		
> > 		try {
> > 			doc.close();
> > 		} catch (IOException e) {
> > 			// TODO Auto-generated catch block
> > 			e.printStackTrace();
> > 		}
> > 	}
> > }
> > `
> >
> > On 2018/05/16 22:44:49, A Brand <a...@awews.com> wrote:
> >> I'd like to add text to an existing pdf document. I attempted it and the
> >> current page was erased and replaced with the text I wanted to add.
> >> I tried using the Overlay() and didn't see the text added.
> >>
> >> Is this a feature of pdfbox?
> >>
> >> Onward.
> >> Don't Be Good. Be Great. Be Grateful.
> >>
> >> Please note this message was edited for brevity and clarity.
> >>
> >> Best regards,
> >> Anthony McDonald
> >>
> >>
> >> *602-456-9335*
> >> http://www.idnkhtw.space
> >>
> > ---------------------------------------------------------------------
> > 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: Add Text To A PDF Document

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 24.05.2018 um 17:11 schrieb a@awews.com:
> I was able to get it to work. I just have to fine tune where the text goes. Here's my solution for anyone needing help in the future.
> Thank you for your help!
> If you have a moment, please let me know if there's a more efficient way to do it.

1) You're using a deprecated method instead of the one I mentioned. This 
will work fine, but some day the method will be removed
2) no need to do a try-catch around every line.

Tilman


>
> `@SpringBootApplication
> public class Pdfbox1Application {
>
> 	public static void main(String[] args) throws IOException {
> 		SpringApplication.run(Pdfbox1Application.class, args);
> 		
> 		File file = new File("./i-90.pdf");
> 		
> 		PDDocument document = null;
> 		PDDocument doc = document .load(file);
> 		PDPage page = doc.getPage(1);
> 		PDPageContentStream.AppendMode appendContent;
> 		PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true);
> 				
> 		doc.setAllSecurityToBeRemoved(true);
> 		
> 		
> 		
> 		try {
> 			contentStream.beginText();
> 		} catch (IOException e) {
> 			// TODO Auto-generated catch block
> 			e.printStackTrace();
> 		}
> 		
> 		try {
> 			contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);
> 			contentStream.newLineAtOffset(25, 700);
> 		} catch (IOException e) {
> 			// TODO Auto-generated catch block
> 			e.printStackTrace();
> 		}
> 		
> 		String text = "This is sample text";
> 		
> 		
> 		
> //		try {
> //			contentStream = new PDPageContentStream(doc, page);
> //		} catch (IOException e) {
> //			// TODO Auto-generated catch block
> //			e.printStackTrace();
> //		}
> 		
> 		
> 		
> 		
> 		
> 		try {
> 			contentStream.showText(text);
> 		} catch (IOException e) {
> 			// TODO Auto-generated catch block
> 			e.printStackTrace();
> 		}
> 		
> 		try {
> 			contentStream.endText();
> 		} catch (IOException e) {
> 			// TODO Auto-generated catch block
> 			e.printStackTrace();
> 		}
> 		
> 		System.out.println("content added");
> 		
> 		try {
> 			contentStream.close();
> 		} catch (IOException e) {
> 			// TODO Auto-generated catch block
> 			e.printStackTrace();
> 		}
> 		
> 		try {
> 			doc.save("./newi-90.pdf");
> 		} catch (IOException e) {
> 			// TODO Auto-generated catch block
> 			e.printStackTrace();
> 		}
> 		
> 		
> 		
> 		try {
> 			doc.close();
> 		} catch (IOException e) {
> 			// TODO Auto-generated catch block
> 			e.printStackTrace();
> 		}
> 	}
> }
> `
>
> On 2018/05/16 22:44:49, A Brand <a...@awews.com> wrote:
>> I'd like to add text to an existing pdf document. I attempted it and the
>> current page was erased and replaced with the text I wanted to add.
>> I tried using the Overlay() and didn't see the text added.
>>
>> Is this a feature of pdfbox?
>>
>> Onward.
>> Don't Be Good. Be Great. Be Grateful.
>>
>> Please note this message was edited for brevity and clarity.
>>
>> Best regards,
>> Anthony McDonald
>>
>>
>> *602-456-9335*
>> http://www.idnkhtw.space
>>
> ---------------------------------------------------------------------
> 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: Add Text To A PDF Document

Posted by a...@awews.com, a...@awews.com.
I was able to get it to work. I just have to fine tune where the text goes. Here's my solution for anyone needing help in the future.
Thank you for your help!
If you have a moment, please let me know if there's a more efficient way to do it.

`@SpringBootApplication
public class Pdfbox1Application {

	public static void main(String[] args) throws IOException {
		SpringApplication.run(Pdfbox1Application.class, args);
		
		File file = new File("./i-90.pdf");
		
		PDDocument document = null;
		PDDocument doc = document .load(file);
		PDPage page = doc.getPage(1);
		PDPageContentStream.AppendMode appendContent;
		PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true);
				
		doc.setAllSecurityToBeRemoved(true);
		
		
		
		try {
			contentStream.beginText();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);
			contentStream.newLineAtOffset(25, 700);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String text = "This is sample text";
		
		
		
//		try {
//			contentStream = new PDPageContentStream(doc, page);
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
		
		
		
		
		try {
			contentStream.showText(text);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			contentStream.endText();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("content added");
		
		try {
			contentStream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			doc.save("./newi-90.pdf");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		try {
			doc.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
`

On 2018/05/16 22:44:49, A Brand <a...@awews.com> wrote: 
> I'd like to add text to an existing pdf document. I attempted it and the
> current page was erased and replaced with the text I wanted to add.
> I tried using the Overlay() and didn't see the text added.
> 
> Is this a feature of pdfbox?
> 
> Onward.
> Don't Be Good. Be Great. Be Grateful.
> 
> Please note this message was edited for brevity and clarity.
> 
> Best regards,
> Anthony McDonald
> 
> 
> *602-456-9335*
> http://www.idnkhtw.space
> 

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