You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Timo Rumland <ti...@digitalys.de> on 2016/05/17 15:04:42 UTC

After using 'PrinterJob' to print a PDF form, the form fields are empty (PDFBox 2.0.0)

Hello,

after migrating from PDFBox 1.8 to 2.0.0, printing a PDF form via
'PrinterJob' and ' PDFPrintable ', in which I filled out the form fields via
PDFBox, results in a PDF file (or a physical print) with the form fields
empty.

With PDFBox 1.8, the following simple code worked:


----------

File sourcePdfForm = new File( "g:/test-form.pdf" );
File resultPdfForm = new File( "g:/test-form_out.pdf" );

// Load the source PDF form file
PDDocument pdDocument = PDDocument.load( sourcePdfForm );
PDDocumentCatalog pdDocumentCatalog = pdDocument.getDocumentCatalog();
PDAcroForm pdAcroForm = pdDocumentCatalog.getAcroForm();

// Setting PDF field content in "textfield01"
PDField pdField = pdAcroForm.getField( "textfield01" );
pdField.setValue( "test content" );

pdDocument.save( resultPdfForm );
pdDocument.close();

// Open the resulting PDF file, which shows the content in the PDF field
"textfield01"
Desktop.getDesktop().open( resultPdfForm );


// Now open the resulting file again, printing it with the default printer
// No matter if the default printer is an PDF printer (like PDFCreator) or
// an actual physical printer, the content of the field "textfield01" is
empty

pdDocument = PDDocument.load( resultPdfForm );

PrinterJob printerJob = PrinterJob.getPrinterJob();
printerJob.setJobName( "printerJob" );
printerJob.setPrintService( PrintServiceLookup.lookupDefaultPrintService()
);   
printerJob.setPrintable( new PDFPrintable( pdDocument ) );
printerJob.print( new HashPrintRequestAttributeSet( MediaSizeName.ISO_A4 )
);

pdDocument.close();

----------

-- What this code sample does --

As you can see, this is a test case where I first load a source PDF form,
setting the value of a form field and save the resulting PDF file. After
that I load the resulting PDF file and use a PrinterJob to print it on the
default printer.


-- The problem --

Now using PDFBox 2.0.0: No matter what the default printer is - a PDF
printer like PDFCreator or a physical printer - the form fields of the
printed out PDF file are empty

Interestingly, opening the resulting PDF form 'test-form_out.pdf' (see line
'Desktop.getDesktop().open( resultPdfForm );') in any PDF viewer (Adobe
Acrobat Reader DC, PDF XChange Viewer etc.) shows the correct form field
content for field 'textfield01', which is 'test content'.

The test PDF form file I use is a really simple form, containing only a
label and the form field named "testfield01". If you wish, you can download
'test-form.pdf' here:

 * https://goo.gl/VMiohj

As mentioned, with PDFBox 1.8 I was able to print out PDF forms via a
'PrinterJob' with form field contents.


-- Related posts --

There are several posts or bug reports regard empty form fields, like these:

 *
http://stackoverflow.com/questions/14541647/pdfbox-set-visible-when-printing
(not providing a valid solution in my case)
 *
http://stackoverflow.com/questions/24149361/form-field-values-set-with-pdfbo
x-not-visible-in-adobe-reader (my PDF form is a AcroForm and has no XFA
data)
 * https://issues.apache.org/jira/browse/PDFBOX-71 (setting
'pdAcroForm.getCOSObject().setItem( COSName.getPDFName( "NeedAppearances" ),
COSBoolean.TRUE );' did not help in my case)

Unfortunately, none of these did help.



Any advice on this problem is highly appreciated



Thanks a lot for your help!


Best regards,
Timo



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


AW: After using 'PrinterJob' to print a PDF form, the form fields are empty (PDFBox 2.0.0)

Posted by Timo Rumland <ti...@digitalys.de>.
Hello,

> The behavior has changed for PDFBox 2.1 (which is in the works - 
> nor release date yet) which will now always generate a new 
> appearance when the form fields value changes.
> BR
> Maruan

thanks for the info, Maruan!


Best Regards,
Timo




-----Ursprüngliche Nachricht-----
Von: Maruan Sahyoun [mailto:sahyoun@fileaffairs.de] 
Gesendet: Freitag, 20. Mai 2016 12:44
An: users@pdfbox.apache.org
Betreff: Re: After using 'PrinterJob' to print a PDF form, the form fields
are empty (PDFBox 2.0.0)

Hi,

> Am 20.05.2016 um 10:43 schrieb Maruan Sahyoun <sa...@fileaffairs.de>:
> 
> Hi,
>> Am 20.05.2016 um 09:49 schrieb Timo Rumland <ti...@digitalys.de>:
>> 
>> Hi Maruan,
>> 
>>> you can change your code like this to use Helvetica.
>>> [...]
>> 
>> you saved my day, sir! It works like a charm, thank you very much.
> 
> Good to hear that,
> 
>> 
>> 
>> To summarize it - I encountered two problems:
>> 
>> 
>> 1. "Invisible" form field content.
>> 
>>   Workaround: Setting 'pdAcroForm.setNeedAppearances( false )', filling 
>>   the fields and setting the property to 'true' again.
> 
> I've created https://issues.apache.org/jira/browse/PDFBOX-3356. Feel free
to add comments, samples etc. if you want.

The behavior has changed for PDFBox 2.1 (which is in the works - nor release
date yet) which will now always generate a new appearance when the form
fields value changes.

BR
Maruan

> 
>> 
>> 2. Wrong encoding / weird special chars in form fields.
>> 
>>  Workaround: Using a PDF default font. In my case, setting the font 
>> of  all form fields to 'PDType1Font.HELVETICA' worked.
>> 
>> 
>> 
>> Can I contribute something to these problems, maybe creating a JIRA 
>> issue (for the first item), or is PDFBOX-3332 sufficient for the moment?
>> 
>> 
>> Thanks again (also to you, John) for all your support
> 
> You're welcome - BR Maruan
>> 
>> 
>> Best Regards,
>> Timo
>> 
>> 
>> 
>> 
>> 
>> -----Ursprüngliche Nachricht-----
>> Von: Maruan Sahyoun [mailto:sahyoun@fileaffairs.de]
>> Gesendet: Mittwoch, 18. Mai 2016 15:03
>> An: users@pdfbox.apache.org
>> Betreff: Re: After using 'PrinterJob' to print a PDF form, the form 
>> fields are empty (PDFBox 2.0.0)
>> 
>> Hi,
>> 
>>> Am 18.05.2016 um 12:13 schrieb Timo Rumland <ti...@digitalys.de>:
>>> 
>>> Hello Maruan,
>>> 
>>>> unfortunately you are hit by
>>> https://issues.apache.org/jira/browse/PDFBOX-3332.
>>>> Will check its state.
>>>> Maruan
>>> 
>>> thanks a lot!
>>> 
>>> In the meantime, is there any known workaround? I think about the
>> following:
>>> 
>>> 
>>> I read in the JIRA ticket, that using the standard PDF font 
>>> Helvetica does not cause these problems.
>>> If this is correct:
>>> 
>>> I can't control the source of the PDF files, but the used font for 
>>> the form fields does not matter in my case. So while I can't force 
>>> the source PDF form to use a specific font, I can modify the used 
>>> font in the PDF form via PDFBox (if possible) before printing.
>>> 
>>> Is there a way to set the default Helvetica font (specified by 
>>> 'PDType1Font.HELVETICA' as far as I know) for all form fields before 
>>> printing? Maybe this is a solution / workaround in my case, so no 
>>> weird characters are displayed?
>> 
>> 
>> you can change your code like this to use Helvetica.
>> 
>>       pdAcroForm.setNeedAppearances(false);
>>       // Setting PDF field content in "textfield01"
>>       PDField pdField = pdAcroForm.getField( "textfield01" );
>>       PDType1Font helv = PDType1Font.HELVETICA;
>>       // add the font to the AcroForm Resources
>>       
>> pdAcroForm.getDefaultResources().put(COSName.getPDFName("Helv"),
>> helv);
>>       // Set the default appearance to use that font.
>>       // Note: this was harcoded from the original value of 
>>       // 0 0 0 rg /F2 12 Tf
>>       // which is not totally correct as the font should be the first 
>> parameter
>>       ((PDTextField)pdField).setDefaultAppearance("/Helv 12 Tf");
>>       pdField.setValue( "test content" );
>>       pdAcroForm.setNeedAppearances(true);
>> 
>> BR
>> Maruan
>> 
>>> 
>>> 
>>> Thanks again!
>>> 
>>> 
>>> Best Regards,
>>> Timo
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> -----Ursprüngliche Nachricht-----
>>> Von: Maruan Sahyoun [mailto:sahyoun@fileaffairs.de]
>>> Gesendet: Mittwoch, 18. Mai 2016 10:43
>>> An: users@pdfbox.apache.org
>>> Betreff: Re: After using 'PrinterJob' to print a PDF form, the form 
>>> fields are empty (PDFBox 2.0.0)
>>> 
>>> Hi,
>>> 
>>>> Am 18.05.2016 um 09:14 schrieb Timo Rumland
<ti...@digitalys.de>:
>>>> 
>>>> Hi Maruan,
>>>> 
>>>>> What you can do is set NeedApparances to false, update the field 
>>>>> and afterwards set the value to true again.
>>>> 
>>>> I've just tested this with the following code (I hope I understood 
>>>> you correctly and my naive approach is sufficient):
>>>> 
>>>> ----------
>>>> 
>>>> pdAcroForm.setNeedAppearances( false ); pdField.setValue( "test 
>>>> content" ); pdAcroForm.setNeedAppearances( true );
>>>> 
>>>> ----------
>>>> 
>>>> (The rest of my sample code is unaltered.)
>>>> 
>>>> Now after running the test code, the form field in the resulting 
>>>> PDF file is NOT empty, but the space between "test" and "content" 
>>>> (the desired field content is "test content") has been replaced 
>>>> with a special character (and the position of the text is slightly
off).
>>>> 
>>>> Please, see this screenshot I made:
>>>> 
>>>> * https://goo.gl/dWUvKy
>>>> 
>>>> There you can see the difference between the source PDF form file 
>>>> and the resulting file after printing.
>>>> 
>>> 
>>> unfortunately you are hit by
>>> https://issues.apache.org/jira/browse/PDFBOX-3332.
>>> 
>>> Will check its state.
>>> Maruan
>>> 
>>> 
>>>> 
>>>> To be honest, I now have absolutely no idea what could cause this, 
>>>> and I really hope you have some more ideas about it, as the problem 
>>>> gets more and more urgent for my current project.
>>>> 
>>>> 
>>>> 
>>>> Thanks a lot for your help!
>>>> 
>>>> 
>>>> 
>>>> Best Regards,
>>>> Timo
>>>> 
>>>> 
>>>> 
>>>> 
>>>> 
>>>> 
>>>> 
>>>> -----Ursprüngliche Nachricht-----
>>>> Von: Maruan Sahyoun [mailto:sahyoun@fileaffairs.de]
>>>> Gesendet: Dienstag, 17. Mai 2016 21:19
>>>> An: users@pdfbox.apache.org
>>>> Betreff: Re: After using 'PrinterJob' to print a PDF form, the form 
>>>> fields are empty (PDFBox 2.0.0)
>>>> 
>>>> Hi,
>>>> 
>>>>> Am 17.05.2016 um 17:04 schrieb Timo Rumland
<ti...@digitalys.de>:
>>>>> 
>>>>> Hello,
>>>>> 
>>>>> after migrating from PDFBox 1.8 to 2.0.0, printing a PDF form via 
>>>>> 'PrinterJob' and ' PDFPrintable ', in which I filled out the form 
>>>>> fields via PDFBox, results in a PDF file (or a physical print) 
>>>>> with the form fields empty.
>>>>> 
>>>> 
>>>> the form fields value is set but the appearance stream for the form 
>>>> field (what you see) is not updated as the NeedAppearances flag is 
>>>> set to
>>> true.
>>>> PDFBox (currently) handles it that it doens't update the appearance 
>>>> stream of the form field in that case (that should probably be
changed).
>>>> 
>>>> What you can do is set NeedApparances to false, update the field 
>>>> and afterwards set the value to true again.
>>>> 
>>>> 
>>>> BR
>>>> Maruan
>>>> 
>>>> 
>>>>> With PDFBox 1.8, the following simple code worked:
>>>>> 
>>>>> 
>>>>> ----------
>>>>> 
>>>>> File sourcePdfForm = new File( "g:/test-form.pdf" ); File 
>>>>> resultPdfForm = new File( "g:/test-form_out.pdf" );
>>>>> 
>>>>> // Load the source PDF form file
>>>>> PDDocument pdDocument = PDDocument.load( sourcePdfForm ); 
>>>>> PDDocumentCatalog pdDocumentCatalog = 
>>>>> pdDocument.getDocumentCatalog(); PDAcroForm pdAcroForm = 
>>>>> pdDocumentCatalog.getAcroForm();
>>>>> 
>>>>> // Setting PDF field content in "textfield01"
>>>>> PDField pdField = pdAcroForm.getField( "textfield01" ); 
>>>>> pdField.setValue( "test content" );
>>>>> 
>>>>> pdDocument.save( resultPdfForm );
>>>>> pdDocument.close();
>>>>> 
>>>>> // Open the resulting PDF file, which shows the content in the PDF 
>>>>> field "textfield01"
>>>>> Desktop.getDesktop().open( resultPdfForm );
>>>>> 
>>>>> 
>>>>> // Now open the resulting file again, printing it with the default 
>>>>> printer // No matter if the default printer is an PDF printer 
>>>>> (like
>>>>> PDFCreator) or // an actual physical printer, the content of the 
>>>>> field "textfield01" is empty
>>>>> 
>>>>> pdDocument = PDDocument.load( resultPdfForm );
>>>>> 
>>>>> PrinterJob printerJob = PrinterJob.getPrinterJob(); 
>>>>> printerJob.setJobName( "printerJob" ); printerJob.setPrintService(
>>>>> PrintServiceLookup.lookupDefaultPrintService()
>>>>> );   
>>>>> printerJob.setPrintable( new PDFPrintable( pdDocument ) ); 
>>>>> printerJob.print( new HashPrintRequestAttributeSet(
>>>>> MediaSizeName.ISO_A4 ) );
>>>>> 
>>>>> pdDocument.close();
>>>>> 
>>>>> ----------
>>>>> 
>>>>> -- What this code sample does --
>>>>> 
>>>>> As you can see, this is a test case where I first load a source 
>>>>> PDF form, setting the value of a form field and save the resulting 
>>>>> PDF file. After that I load the resulting PDF file and use a 
>>>>> PrinterJob to print it on the default printer.
>>>>> 
>>>>> 
>>>>> -- The problem --
>>>>> 
>>>>> Now using PDFBox 2.0.0: No matter what the default printer is - a 
>>>>> PDF printer like PDFCreator or a physical printer - the form 
>>>>> fields of the printed out PDF file are empty
>>>>> 
>>>>> Interestingly, opening the resulting PDF form 'test-form_out.pdf' 
>>>>> (see line 'Desktop.getDesktop().open( resultPdfForm );') in any 
>>>>> PDF viewer (Adobe Acrobat Reader DC, PDF XChange Viewer etc.) 
>>>>> shows the correct form field content for field 'textfield01', 
>>>>> which is 'test
>>> content'.
>>>>> 
>>>>> The test PDF form file I use is a really simple form, containing 
>>>>> only a label and the form field named "testfield01". If you wish, 
>>>>> you can download 'test-form.pdf' here:
>>>>> 
>>>>> * https://goo.gl/VMiohj
>>>>> 
>>>>> As mentioned, with PDFBox 1.8 I was able to print out PDF forms 
>>>>> via a 'PrinterJob' with form field contents.
>>>>> 
>>>>> 
>>>>> -- Related posts --
>>>>> 
>>>>> There are several posts or bug reports regard empty form fields, 
>>>>> like
>>>> these:
>>>>> 
>>>>> *
>>>>> http://stackoverflow.com/questions/14541647/pdfbox-set-visible-whe
>>>>> n- p r inting (not providing a valid solution in my case)
>>>>> *
>>>>> http://stackoverflow.com/questions/24149361/form-field-values-set-
>>>>> wi t h -pdfbo x-not-visible-in-adobe-reader (my PDF form is a 
>>>>> AcroForm and has no XFA
>>>>> data)
>>>>> * https://issues.apache.org/jira/browse/PDFBOX-71 (setting 
>>>>> 'pdAcroForm.getCOSObject().setItem( COSName.getPDFName( 
>>>>> "NeedAppearances" ), COSBoolean.TRUE );' did not help in my case)
>>>>> 
>>>>> Unfortunately, none of these did help.
>>>>> 
>>>>> 
>>>>> 
>>>>> Any advice on this problem is highly appreciated
>>>>> 
>>>>> 
>>>>> 
>>>>> Thanks a lot for your help!
>>>>> 
>>>>> 
>>>>> Best regards,
>>>>> Timo
>>>>> 
>>>>> 
>>>>> 
>>>>> ------------------------------------------------------------------
>>>>> --
>>>>> - 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
>>> 
>>> 
>>> 
>>> --------------------------------------------------------------------
>>> - 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
> 


---------------------------------------------------------------------
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: After using 'PrinterJob' to print a PDF form, the form fields are empty (PDFBox 2.0.0)

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

> Am 20.05.2016 um 10:43 schrieb Maruan Sahyoun <sa...@fileaffairs.de>:
> 
> Hi,
>> Am 20.05.2016 um 09:49 schrieb Timo Rumland <ti...@digitalys.de>:
>> 
>> Hi Maruan,
>> 
>>> you can change your code like this to use Helvetica.
>>> [...]
>> 
>> you saved my day, sir! It works like a charm, thank you very much.
> 
> Good to hear that,
> 
>> 
>> 
>> To summarize it - I encountered two problems:
>> 
>> 
>> 1. "Invisible" form field content.
>> 
>>   Workaround: Setting 'pdAcroForm.setNeedAppearances( false )', filling 
>>   the fields and setting the property to 'true' again.
> 
> I've created https://issues.apache.org/jira/browse/PDFBOX-3356. Feel free to add comments, samples etc. if you want.

The behavior has changed for PDFBox 2.1 (which is in the works - nor release date yet) which will now always generate a new appearance when the form fields value changes.

BR
Maruan

> 
>> 
>> 2. Wrong encoding / weird special chars in form fields.
>> 
>>  Workaround: Using a PDF default font. In my case, setting the font of 
>>  all form fields to 'PDType1Font.HELVETICA' worked.
>> 
>> 
>> 
>> Can I contribute something to these problems, maybe creating a JIRA issue
>> (for the first item), or is PDFBOX-3332 sufficient for the moment?
>> 
>> 
>> Thanks again (also to you, John) for all your support
> 
> You're welcome - BR Maruan
>> 
>> 
>> Best Regards,
>> Timo
>> 
>> 
>> 
>> 
>> 
>> -----Ursprüngliche Nachricht-----
>> Von: Maruan Sahyoun [mailto:sahyoun@fileaffairs.de] 
>> Gesendet: Mittwoch, 18. Mai 2016 15:03
>> An: users@pdfbox.apache.org
>> Betreff: Re: After using 'PrinterJob' to print a PDF form, the form fields
>> are empty (PDFBox 2.0.0)
>> 
>> Hi,
>> 
>>> Am 18.05.2016 um 12:13 schrieb Timo Rumland <ti...@digitalys.de>:
>>> 
>>> Hello Maruan,
>>> 
>>>> unfortunately you are hit by
>>> https://issues.apache.org/jira/browse/PDFBOX-3332.
>>>> Will check its state.
>>>> Maruan
>>> 
>>> thanks a lot!
>>> 
>>> In the meantime, is there any known workaround? I think about the
>> following:
>>> 
>>> 
>>> I read in the JIRA ticket, that using the standard PDF font Helvetica 
>>> does not cause these problems.
>>> If this is correct:
>>> 
>>> I can't control the source of the PDF files, but the used font for the 
>>> form fields does not matter in my case. So while I can't force the 
>>> source PDF form to use a specific font, I can modify the used font in 
>>> the PDF form via PDFBox (if possible) before printing.
>>> 
>>> Is there a way to set the default Helvetica font (specified by 
>>> 'PDType1Font.HELVETICA' as far as I know) for all form fields before 
>>> printing? Maybe this is a solution / workaround in my case, so no 
>>> weird characters are displayed?
>> 
>> 
>> you can change your code like this to use Helvetica.
>> 
>>       pdAcroForm.setNeedAppearances(false);
>>       // Setting PDF field content in "textfield01"
>>       PDField pdField = pdAcroForm.getField( "textfield01" );
>>       PDType1Font helv = PDType1Font.HELVETICA;
>>       // add the font to the AcroForm Resources
>>       pdAcroForm.getDefaultResources().put(COSName.getPDFName("Helv"),
>> helv);
>>       // Set the default appearance to use that font.
>>       // Note: this was harcoded from the original value of 
>>       // 0 0 0 rg /F2 12 Tf
>>       // which is not totally correct as the font should be the first
>> parameter
>>       ((PDTextField)pdField).setDefaultAppearance("/Helv 12 Tf");
>>       pdField.setValue( "test content" );
>>       pdAcroForm.setNeedAppearances(true);
>> 
>> BR
>> Maruan
>> 
>>> 
>>> 
>>> Thanks again!
>>> 
>>> 
>>> Best Regards,
>>> Timo
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> -----Ursprüngliche Nachricht-----
>>> Von: Maruan Sahyoun [mailto:sahyoun@fileaffairs.de]
>>> Gesendet: Mittwoch, 18. Mai 2016 10:43
>>> An: users@pdfbox.apache.org
>>> Betreff: Re: After using 'PrinterJob' to print a PDF form, the form 
>>> fields are empty (PDFBox 2.0.0)
>>> 
>>> Hi,
>>> 
>>>> Am 18.05.2016 um 09:14 schrieb Timo Rumland <ti...@digitalys.de>:
>>>> 
>>>> Hi Maruan,
>>>> 
>>>>> What you can do is set NeedApparances to false, update the field and 
>>>>> afterwards set the value to true again.
>>>> 
>>>> I've just tested this with the following code (I hope I understood 
>>>> you correctly and my naive approach is sufficient):
>>>> 
>>>> ----------
>>>> 
>>>> pdAcroForm.setNeedAppearances( false ); pdField.setValue( "test 
>>>> content" ); pdAcroForm.setNeedAppearances( true );
>>>> 
>>>> ----------
>>>> 
>>>> (The rest of my sample code is unaltered.)
>>>> 
>>>> Now after running the test code, the form field in the resulting PDF 
>>>> file is NOT empty, but the space between "test" and "content" (the 
>>>> desired field content is "test content") has been replaced with a 
>>>> special character (and the position of the text is slightly off).
>>>> 
>>>> Please, see this screenshot I made:
>>>> 
>>>> * https://goo.gl/dWUvKy
>>>> 
>>>> There you can see the difference between the source PDF form file and 
>>>> the resulting file after printing.
>>>> 
>>> 
>>> unfortunately you are hit by
>>> https://issues.apache.org/jira/browse/PDFBOX-3332.
>>> 
>>> Will check its state.
>>> Maruan
>>> 
>>> 
>>>> 
>>>> To be honest, I now have absolutely no idea what could cause this, 
>>>> and I really hope you have some more ideas about it, as the problem 
>>>> gets more and more urgent for my current project.
>>>> 
>>>> 
>>>> 
>>>> Thanks a lot for your help!
>>>> 
>>>> 
>>>> 
>>>> Best Regards,
>>>> Timo
>>>> 
>>>> 
>>>> 
>>>> 
>>>> 
>>>> 
>>>> 
>>>> -----Ursprüngliche Nachricht-----
>>>> Von: Maruan Sahyoun [mailto:sahyoun@fileaffairs.de]
>>>> Gesendet: Dienstag, 17. Mai 2016 21:19
>>>> An: users@pdfbox.apache.org
>>>> Betreff: Re: After using 'PrinterJob' to print a PDF form, the form 
>>>> fields are empty (PDFBox 2.0.0)
>>>> 
>>>> Hi,
>>>> 
>>>>> Am 17.05.2016 um 17:04 schrieb Timo Rumland <ti...@digitalys.de>:
>>>>> 
>>>>> Hello,
>>>>> 
>>>>> after migrating from PDFBox 1.8 to 2.0.0, printing a PDF form via 
>>>>> 'PrinterJob' and ' PDFPrintable ', in which I filled out the form 
>>>>> fields via PDFBox, results in a PDF file (or a physical print) with 
>>>>> the form fields empty.
>>>>> 
>>>> 
>>>> the form fields value is set but the appearance stream for the form 
>>>> field (what you see) is not updated as the NeedAppearances flag is 
>>>> set to
>>> true.
>>>> PDFBox (currently) handles it that it doens't update the appearance 
>>>> stream of the form field in that case (that should probably be changed).
>>>> 
>>>> What you can do is set NeedApparances to false, update the field and 
>>>> afterwards set the value to true again.
>>>> 
>>>> 
>>>> BR
>>>> Maruan
>>>> 
>>>> 
>>>>> With PDFBox 1.8, the following simple code worked:
>>>>> 
>>>>> 
>>>>> ----------
>>>>> 
>>>>> File sourcePdfForm = new File( "g:/test-form.pdf" ); File 
>>>>> resultPdfForm = new File( "g:/test-form_out.pdf" );
>>>>> 
>>>>> // Load the source PDF form file
>>>>> PDDocument pdDocument = PDDocument.load( sourcePdfForm ); 
>>>>> PDDocumentCatalog pdDocumentCatalog = 
>>>>> pdDocument.getDocumentCatalog(); PDAcroForm pdAcroForm = 
>>>>> pdDocumentCatalog.getAcroForm();
>>>>> 
>>>>> // Setting PDF field content in "textfield01"
>>>>> PDField pdField = pdAcroForm.getField( "textfield01" ); 
>>>>> pdField.setValue( "test content" );
>>>>> 
>>>>> pdDocument.save( resultPdfForm );
>>>>> pdDocument.close();
>>>>> 
>>>>> // Open the resulting PDF file, which shows the content in the PDF 
>>>>> field "textfield01"
>>>>> Desktop.getDesktop().open( resultPdfForm );
>>>>> 
>>>>> 
>>>>> // Now open the resulting file again, printing it with the default 
>>>>> printer // No matter if the default printer is an PDF printer (like
>>>>> PDFCreator) or // an actual physical printer, the content of the 
>>>>> field "textfield01" is empty
>>>>> 
>>>>> pdDocument = PDDocument.load( resultPdfForm );
>>>>> 
>>>>> PrinterJob printerJob = PrinterJob.getPrinterJob(); 
>>>>> printerJob.setJobName( "printerJob" ); printerJob.setPrintService(
>>>>> PrintServiceLookup.lookupDefaultPrintService()
>>>>> );   
>>>>> printerJob.setPrintable( new PDFPrintable( pdDocument ) ); 
>>>>> printerJob.print( new HashPrintRequestAttributeSet(
>>>>> MediaSizeName.ISO_A4 ) );
>>>>> 
>>>>> pdDocument.close();
>>>>> 
>>>>> ----------
>>>>> 
>>>>> -- What this code sample does --
>>>>> 
>>>>> As you can see, this is a test case where I first load a source PDF 
>>>>> form, setting the value of a form field and save the resulting PDF 
>>>>> file. After that I load the resulting PDF file and use a PrinterJob 
>>>>> to print it on the default printer.
>>>>> 
>>>>> 
>>>>> -- The problem --
>>>>> 
>>>>> Now using PDFBox 2.0.0: No matter what the default printer is - a 
>>>>> PDF printer like PDFCreator or a physical printer - the form fields 
>>>>> of the printed out PDF file are empty
>>>>> 
>>>>> Interestingly, opening the resulting PDF form 'test-form_out.pdf' 
>>>>> (see line 'Desktop.getDesktop().open( resultPdfForm );') in any PDF 
>>>>> viewer (Adobe Acrobat Reader DC, PDF XChange Viewer etc.) shows the 
>>>>> correct form field content for field 'textfield01', which is 'test
>>> content'.
>>>>> 
>>>>> The test PDF form file I use is a really simple form, containing 
>>>>> only a label and the form field named "testfield01". If you wish, 
>>>>> you can download 'test-form.pdf' here:
>>>>> 
>>>>> * https://goo.gl/VMiohj
>>>>> 
>>>>> As mentioned, with PDFBox 1.8 I was able to print out PDF forms via 
>>>>> a 'PrinterJob' with form field contents.
>>>>> 
>>>>> 
>>>>> -- Related posts --
>>>>> 
>>>>> There are several posts or bug reports regard empty form fields, 
>>>>> like
>>>> these:
>>>>> 
>>>>> *
>>>>> http://stackoverflow.com/questions/14541647/pdfbox-set-visible-when-
>>>>> p r inting (not providing a valid solution in my case)
>>>>> *
>>>>> http://stackoverflow.com/questions/24149361/form-field-values-set-wi
>>>>> t h -pdfbo x-not-visible-in-adobe-reader (my PDF form is a AcroForm 
>>>>> and has no XFA
>>>>> data)
>>>>> * https://issues.apache.org/jira/browse/PDFBOX-71 (setting 
>>>>> 'pdAcroForm.getCOSObject().setItem( COSName.getPDFName( 
>>>>> "NeedAppearances" ), COSBoolean.TRUE );' did not help in my case)
>>>>> 
>>>>> Unfortunately, none of these did help.
>>>>> 
>>>>> 
>>>>> 
>>>>> Any advice on this problem is highly appreciated
>>>>> 
>>>>> 
>>>>> 
>>>>> Thanks a lot for your help!
>>>>> 
>>>>> 
>>>>> Best regards,
>>>>> Timo
>>>>> 
>>>>> 
>>>>> 
>>>>> --------------------------------------------------------------------
>>>>> - 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
>>> 
>>> 
>>> 
>>> ---------------------------------------------------------------------
>>> 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
> 


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


Re: After using 'PrinterJob' to print a PDF form, the form fields are empty (PDFBox 2.0.0)

Posted by Maruan Sahyoun <sa...@fileaffairs.de>.
Hi,
> Am 20.05.2016 um 09:49 schrieb Timo Rumland <ti...@digitalys.de>:
> 
> Hi Maruan,
> 
>> you can change your code like this to use Helvetica.
>> [...]
> 
> you saved my day, sir! It works like a charm, thank you very much.

Good to hear that,

> 
> 
> To summarize it - I encountered two problems:
> 
> 
> 1. "Invisible" form field content.
> 
>    Workaround: Setting 'pdAcroForm.setNeedAppearances( false )', filling 
>    the fields and setting the property to 'true' again.

I've created https://issues.apache.org/jira/browse/PDFBOX-3356. Feel free to add comments, samples etc. if you want.

> 
> 2. Wrong encoding / weird special chars in form fields.
> 
>   Workaround: Using a PDF default font. In my case, setting the font of 
>   all form fields to 'PDType1Font.HELVETICA' worked.
> 
> 
> 
> Can I contribute something to these problems, maybe creating a JIRA issue
> (for the first item), or is PDFBOX-3332 sufficient for the moment?
> 
> 
> Thanks again (also to you, John) for all your support

You're welcome - BR Maruan
> 
> 
> Best Regards,
> Timo
> 
> 
> 
> 
> 
> -----Ursprüngliche Nachricht-----
> Von: Maruan Sahyoun [mailto:sahyoun@fileaffairs.de] 
> Gesendet: Mittwoch, 18. Mai 2016 15:03
> An: users@pdfbox.apache.org
> Betreff: Re: After using 'PrinterJob' to print a PDF form, the form fields
> are empty (PDFBox 2.0.0)
> 
> Hi,
> 
>> Am 18.05.2016 um 12:13 schrieb Timo Rumland <ti...@digitalys.de>:
>> 
>> Hello Maruan,
>> 
>>> unfortunately you are hit by
>> https://issues.apache.org/jira/browse/PDFBOX-3332.
>>> Will check its state.
>>> Maruan
>> 
>> thanks a lot!
>> 
>> In the meantime, is there any known workaround? I think about the
> following:
>> 
>> 
>> I read in the JIRA ticket, that using the standard PDF font Helvetica 
>> does not cause these problems.
>> If this is correct:
>> 
>> I can't control the source of the PDF files, but the used font for the 
>> form fields does not matter in my case. So while I can't force the 
>> source PDF form to use a specific font, I can modify the used font in 
>> the PDF form via PDFBox (if possible) before printing.
>> 
>> Is there a way to set the default Helvetica font (specified by 
>> 'PDType1Font.HELVETICA' as far as I know) for all form fields before 
>> printing? Maybe this is a solution / workaround in my case, so no 
>> weird characters are displayed?
> 
> 
> you can change your code like this to use Helvetica.
> 
>        pdAcroForm.setNeedAppearances(false);
>        // Setting PDF field content in "textfield01"
>        PDField pdField = pdAcroForm.getField( "textfield01" );
>        PDType1Font helv = PDType1Font.HELVETICA;
>        // add the font to the AcroForm Resources
>        pdAcroForm.getDefaultResources().put(COSName.getPDFName("Helv"),
> helv);
>        // Set the default appearance to use that font.
>        // Note: this was harcoded from the original value of 
>        // 0 0 0 rg /F2 12 Tf
>        // which is not totally correct as the font should be the first
> parameter
>        ((PDTextField)pdField).setDefaultAppearance("/Helv 12 Tf");
>        pdField.setValue( "test content" );
>        pdAcroForm.setNeedAppearances(true);
> 
> BR
> Maruan
> 
>> 
>> 
>> Thanks again!
>> 
>> 
>> Best Regards,
>> Timo
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> -----Ursprüngliche Nachricht-----
>> Von: Maruan Sahyoun [mailto:sahyoun@fileaffairs.de]
>> Gesendet: Mittwoch, 18. Mai 2016 10:43
>> An: users@pdfbox.apache.org
>> Betreff: Re: After using 'PrinterJob' to print a PDF form, the form 
>> fields are empty (PDFBox 2.0.0)
>> 
>> Hi,
>> 
>>> Am 18.05.2016 um 09:14 schrieb Timo Rumland <ti...@digitalys.de>:
>>> 
>>> Hi Maruan,
>>> 
>>>> What you can do is set NeedApparances to false, update the field and 
>>>> afterwards set the value to true again.
>>> 
>>> I've just tested this with the following code (I hope I understood 
>>> you correctly and my naive approach is sufficient):
>>> 
>>> ----------
>>> 
>>> pdAcroForm.setNeedAppearances( false ); pdField.setValue( "test 
>>> content" ); pdAcroForm.setNeedAppearances( true );
>>> 
>>> ----------
>>> 
>>> (The rest of my sample code is unaltered.)
>>> 
>>> Now after running the test code, the form field in the resulting PDF 
>>> file is NOT empty, but the space between "test" and "content" (the 
>>> desired field content is "test content") has been replaced with a 
>>> special character (and the position of the text is slightly off).
>>> 
>>> Please, see this screenshot I made:
>>> 
>>> * https://goo.gl/dWUvKy
>>> 
>>> There you can see the difference between the source PDF form file and 
>>> the resulting file after printing.
>>> 
>> 
>> unfortunately you are hit by
>> https://issues.apache.org/jira/browse/PDFBOX-3332.
>> 
>> Will check its state.
>> Maruan
>> 
>> 
>>> 
>>> To be honest, I now have absolutely no idea what could cause this, 
>>> and I really hope you have some more ideas about it, as the problem 
>>> gets more and more urgent for my current project.
>>> 
>>> 
>>> 
>>> Thanks a lot for your help!
>>> 
>>> 
>>> 
>>> Best Regards,
>>> Timo
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> -----Ursprüngliche Nachricht-----
>>> Von: Maruan Sahyoun [mailto:sahyoun@fileaffairs.de]
>>> Gesendet: Dienstag, 17. Mai 2016 21:19
>>> An: users@pdfbox.apache.org
>>> Betreff: Re: After using 'PrinterJob' to print a PDF form, the form 
>>> fields are empty (PDFBox 2.0.0)
>>> 
>>> Hi,
>>> 
>>>> Am 17.05.2016 um 17:04 schrieb Timo Rumland <ti...@digitalys.de>:
>>>> 
>>>> Hello,
>>>> 
>>>> after migrating from PDFBox 1.8 to 2.0.0, printing a PDF form via 
>>>> 'PrinterJob' and ' PDFPrintable ', in which I filled out the form 
>>>> fields via PDFBox, results in a PDF file (or a physical print) with 
>>>> the form fields empty.
>>>> 
>>> 
>>> the form fields value is set but the appearance stream for the form 
>>> field (what you see) is not updated as the NeedAppearances flag is 
>>> set to
>> true.
>>> PDFBox (currently) handles it that it doens't update the appearance 
>>> stream of the form field in that case (that should probably be changed).
>>> 
>>> What you can do is set NeedApparances to false, update the field and 
>>> afterwards set the value to true again.
>>> 
>>> 
>>> BR
>>> Maruan
>>> 
>>> 
>>>> With PDFBox 1.8, the following simple code worked:
>>>> 
>>>> 
>>>> ----------
>>>> 
>>>> File sourcePdfForm = new File( "g:/test-form.pdf" ); File 
>>>> resultPdfForm = new File( "g:/test-form_out.pdf" );
>>>> 
>>>> // Load the source PDF form file
>>>> PDDocument pdDocument = PDDocument.load( sourcePdfForm ); 
>>>> PDDocumentCatalog pdDocumentCatalog = 
>>>> pdDocument.getDocumentCatalog(); PDAcroForm pdAcroForm = 
>>>> pdDocumentCatalog.getAcroForm();
>>>> 
>>>> // Setting PDF field content in "textfield01"
>>>> PDField pdField = pdAcroForm.getField( "textfield01" ); 
>>>> pdField.setValue( "test content" );
>>>> 
>>>> pdDocument.save( resultPdfForm );
>>>> pdDocument.close();
>>>> 
>>>> // Open the resulting PDF file, which shows the content in the PDF 
>>>> field "textfield01"
>>>> Desktop.getDesktop().open( resultPdfForm );
>>>> 
>>>> 
>>>> // Now open the resulting file again, printing it with the default 
>>>> printer // No matter if the default printer is an PDF printer (like
>>>> PDFCreator) or // an actual physical printer, the content of the 
>>>> field "textfield01" is empty
>>>> 
>>>> pdDocument = PDDocument.load( resultPdfForm );
>>>> 
>>>> PrinterJob printerJob = PrinterJob.getPrinterJob(); 
>>>> printerJob.setJobName( "printerJob" ); printerJob.setPrintService(
>>>> PrintServiceLookup.lookupDefaultPrintService()
>>>> );   
>>>> printerJob.setPrintable( new PDFPrintable( pdDocument ) ); 
>>>> printerJob.print( new HashPrintRequestAttributeSet(
>>>> MediaSizeName.ISO_A4 ) );
>>>> 
>>>> pdDocument.close();
>>>> 
>>>> ----------
>>>> 
>>>> -- What this code sample does --
>>>> 
>>>> As you can see, this is a test case where I first load a source PDF 
>>>> form, setting the value of a form field and save the resulting PDF 
>>>> file. After that I load the resulting PDF file and use a PrinterJob 
>>>> to print it on the default printer.
>>>> 
>>>> 
>>>> -- The problem --
>>>> 
>>>> Now using PDFBox 2.0.0: No matter what the default printer is - a 
>>>> PDF printer like PDFCreator or a physical printer - the form fields 
>>>> of the printed out PDF file are empty
>>>> 
>>>> Interestingly, opening the resulting PDF form 'test-form_out.pdf' 
>>>> (see line 'Desktop.getDesktop().open( resultPdfForm );') in any PDF 
>>>> viewer (Adobe Acrobat Reader DC, PDF XChange Viewer etc.) shows the 
>>>> correct form field content for field 'textfield01', which is 'test
>> content'.
>>>> 
>>>> The test PDF form file I use is a really simple form, containing 
>>>> only a label and the form field named "testfield01". If you wish, 
>>>> you can download 'test-form.pdf' here:
>>>> 
>>>> * https://goo.gl/VMiohj
>>>> 
>>>> As mentioned, with PDFBox 1.8 I was able to print out PDF forms via 
>>>> a 'PrinterJob' with form field contents.
>>>> 
>>>> 
>>>> -- Related posts --
>>>> 
>>>> There are several posts or bug reports regard empty form fields, 
>>>> like
>>> these:
>>>> 
>>>> *
>>>> http://stackoverflow.com/questions/14541647/pdfbox-set-visible-when-
>>>> p r inting (not providing a valid solution in my case)
>>>> *
>>>> http://stackoverflow.com/questions/24149361/form-field-values-set-wi
>>>> t h -pdfbo x-not-visible-in-adobe-reader (my PDF form is a AcroForm 
>>>> and has no XFA
>>>> data)
>>>> * https://issues.apache.org/jira/browse/PDFBOX-71 (setting 
>>>> 'pdAcroForm.getCOSObject().setItem( COSName.getPDFName( 
>>>> "NeedAppearances" ), COSBoolean.TRUE );' did not help in my case)
>>>> 
>>>> Unfortunately, none of these did help.
>>>> 
>>>> 
>>>> 
>>>> Any advice on this problem is highly appreciated
>>>> 
>>>> 
>>>> 
>>>> Thanks a lot for your help!
>>>> 
>>>> 
>>>> Best regards,
>>>> Timo
>>>> 
>>>> 
>>>> 
>>>> --------------------------------------------------------------------
>>>> - 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
>> 
>> 
>> 
>> ---------------------------------------------------------------------
>> 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


AW: After using 'PrinterJob' to print a PDF form, the form fields are empty (PDFBox 2.0.0)

Posted by Timo Rumland <ti...@digitalys.de>.
Hi Maruan,

> you can change your code like this to use Helvetica.
> [...]

you saved my day, sir! It works like a charm, thank you very much.


To summarize it - I encountered two problems:


1. "Invisible" form field content.
    
    Workaround: Setting 'pdAcroForm.setNeedAppearances( false )', filling 
    the fields and setting the property to 'true' again.

2. Wrong encoding / weird special chars in form fields.

   Workaround: Using a PDF default font. In my case, setting the font of 
   all form fields to 'PDType1Font.HELVETICA' worked.



Can I contribute something to these problems, maybe creating a JIRA issue
(for the first item), or is PDFBOX-3332 sufficient for the moment?


Thanks again (also to you, John) for all your support


Best Regards,
Timo





-----Ursprüngliche Nachricht-----
Von: Maruan Sahyoun [mailto:sahyoun@fileaffairs.de] 
Gesendet: Mittwoch, 18. Mai 2016 15:03
An: users@pdfbox.apache.org
Betreff: Re: After using 'PrinterJob' to print a PDF form, the form fields
are empty (PDFBox 2.0.0)

Hi,

> Am 18.05.2016 um 12:13 schrieb Timo Rumland <ti...@digitalys.de>:
> 
> Hello Maruan,
> 
>> unfortunately you are hit by
> https://issues.apache.org/jira/browse/PDFBOX-3332.
>> Will check its state.
>> Maruan
> 
> thanks a lot!
> 
> In the meantime, is there any known workaround? I think about the
following:
> 
> 
> I read in the JIRA ticket, that using the standard PDF font Helvetica 
> does not cause these problems.
> If this is correct:
> 
> I can't control the source of the PDF files, but the used font for the 
> form fields does not matter in my case. So while I can't force the 
> source PDF form to use a specific font, I can modify the used font in 
> the PDF form via PDFBox (if possible) before printing.
> 
> Is there a way to set the default Helvetica font (specified by 
> 'PDType1Font.HELVETICA' as far as I know) for all form fields before 
> printing? Maybe this is a solution / workaround in my case, so no 
> weird characters are displayed?


you can change your code like this to use Helvetica.

        pdAcroForm.setNeedAppearances(false);
        // Setting PDF field content in "textfield01"
        PDField pdField = pdAcroForm.getField( "textfield01" );
        PDType1Font helv = PDType1Font.HELVETICA;
        // add the font to the AcroForm Resources
        pdAcroForm.getDefaultResources().put(COSName.getPDFName("Helv"),
helv);
        // Set the default appearance to use that font.
        // Note: this was harcoded from the original value of 
        // 0 0 0 rg /F2 12 Tf
        // which is not totally correct as the font should be the first
parameter
        ((PDTextField)pdField).setDefaultAppearance("/Helv 12 Tf");
        pdField.setValue( "test content" );
        pdAcroForm.setNeedAppearances(true);

BR
Maruan

> 
> 
> Thanks again!
> 
> 
> Best Regards,
> Timo
> 
> 
> 
> 
> 
> 
> 
> 
> -----Ursprüngliche Nachricht-----
> Von: Maruan Sahyoun [mailto:sahyoun@fileaffairs.de]
> Gesendet: Mittwoch, 18. Mai 2016 10:43
> An: users@pdfbox.apache.org
> Betreff: Re: After using 'PrinterJob' to print a PDF form, the form 
> fields are empty (PDFBox 2.0.0)
> 
> Hi,
> 
>> Am 18.05.2016 um 09:14 schrieb Timo Rumland <ti...@digitalys.de>:
>> 
>> Hi Maruan,
>> 
>>> What you can do is set NeedApparances to false, update the field and 
>>> afterwards set the value to true again.
>> 
>> I've just tested this with the following code (I hope I understood 
>> you correctly and my naive approach is sufficient):
>> 
>> ----------
>> 
>> pdAcroForm.setNeedAppearances( false ); pdField.setValue( "test 
>> content" ); pdAcroForm.setNeedAppearances( true );
>> 
>> ----------
>> 
>> (The rest of my sample code is unaltered.)
>> 
>> Now after running the test code, the form field in the resulting PDF 
>> file is NOT empty, but the space between "test" and "content" (the 
>> desired field content is "test content") has been replaced with a 
>> special character (and the position of the text is slightly off).
>> 
>> Please, see this screenshot I made:
>> 
>> * https://goo.gl/dWUvKy
>> 
>> There you can see the difference between the source PDF form file and 
>> the resulting file after printing.
>> 
> 
> unfortunately you are hit by
> https://issues.apache.org/jira/browse/PDFBOX-3332.
> 
> Will check its state.
> Maruan
> 
> 
>> 
>> To be honest, I now have absolutely no idea what could cause this, 
>> and I really hope you have some more ideas about it, as the problem 
>> gets more and more urgent for my current project.
>> 
>> 
>> 
>> Thanks a lot for your help!
>> 
>> 
>> 
>> Best Regards,
>> Timo
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> -----Ursprüngliche Nachricht-----
>> Von: Maruan Sahyoun [mailto:sahyoun@fileaffairs.de]
>> Gesendet: Dienstag, 17. Mai 2016 21:19
>> An: users@pdfbox.apache.org
>> Betreff: Re: After using 'PrinterJob' to print a PDF form, the form 
>> fields are empty (PDFBox 2.0.0)
>> 
>> Hi,
>> 
>>> Am 17.05.2016 um 17:04 schrieb Timo Rumland <ti...@digitalys.de>:
>>> 
>>> Hello,
>>> 
>>> after migrating from PDFBox 1.8 to 2.0.0, printing a PDF form via 
>>> 'PrinterJob' and ' PDFPrintable ', in which I filled out the form 
>>> fields via PDFBox, results in a PDF file (or a physical print) with 
>>> the form fields empty.
>>> 
>> 
>> the form fields value is set but the appearance stream for the form 
>> field (what you see) is not updated as the NeedAppearances flag is 
>> set to
> true.
>> PDFBox (currently) handles it that it doens't update the appearance 
>> stream of the form field in that case (that should probably be changed).
>> 
>> What you can do is set NeedApparances to false, update the field and 
>> afterwards set the value to true again.
>> 
>> 
>> BR
>> Maruan
>> 
>> 
>>> With PDFBox 1.8, the following simple code worked:
>>> 
>>> 
>>> ----------
>>> 
>>> File sourcePdfForm = new File( "g:/test-form.pdf" ); File 
>>> resultPdfForm = new File( "g:/test-form_out.pdf" );
>>> 
>>> // Load the source PDF form file
>>> PDDocument pdDocument = PDDocument.load( sourcePdfForm ); 
>>> PDDocumentCatalog pdDocumentCatalog = 
>>> pdDocument.getDocumentCatalog(); PDAcroForm pdAcroForm = 
>>> pdDocumentCatalog.getAcroForm();
>>> 
>>> // Setting PDF field content in "textfield01"
>>> PDField pdField = pdAcroForm.getField( "textfield01" ); 
>>> pdField.setValue( "test content" );
>>> 
>>> pdDocument.save( resultPdfForm );
>>> pdDocument.close();
>>> 
>>> // Open the resulting PDF file, which shows the content in the PDF 
>>> field "textfield01"
>>> Desktop.getDesktop().open( resultPdfForm );
>>> 
>>> 
>>> // Now open the resulting file again, printing it with the default 
>>> printer // No matter if the default printer is an PDF printer (like
>>> PDFCreator) or // an actual physical printer, the content of the 
>>> field "textfield01" is empty
>>> 
>>> pdDocument = PDDocument.load( resultPdfForm );
>>> 
>>> PrinterJob printerJob = PrinterJob.getPrinterJob(); 
>>> printerJob.setJobName( "printerJob" ); printerJob.setPrintService(
>>> PrintServiceLookup.lookupDefaultPrintService()
>>> );   
>>> printerJob.setPrintable( new PDFPrintable( pdDocument ) ); 
>>> printerJob.print( new HashPrintRequestAttributeSet(
>>> MediaSizeName.ISO_A4 ) );
>>> 
>>> pdDocument.close();
>>> 
>>> ----------
>>> 
>>> -- What this code sample does --
>>> 
>>> As you can see, this is a test case where I first load a source PDF 
>>> form, setting the value of a form field and save the resulting PDF 
>>> file. After that I load the resulting PDF file and use a PrinterJob 
>>> to print it on the default printer.
>>> 
>>> 
>>> -- The problem --
>>> 
>>> Now using PDFBox 2.0.0: No matter what the default printer is - a 
>>> PDF printer like PDFCreator or a physical printer - the form fields 
>>> of the printed out PDF file are empty
>>> 
>>> Interestingly, opening the resulting PDF form 'test-form_out.pdf' 
>>> (see line 'Desktop.getDesktop().open( resultPdfForm );') in any PDF 
>>> viewer (Adobe Acrobat Reader DC, PDF XChange Viewer etc.) shows the 
>>> correct form field content for field 'textfield01', which is 'test
> content'.
>>> 
>>> The test PDF form file I use is a really simple form, containing 
>>> only a label and the form field named "testfield01". If you wish, 
>>> you can download 'test-form.pdf' here:
>>> 
>>> * https://goo.gl/VMiohj
>>> 
>>> As mentioned, with PDFBox 1.8 I was able to print out PDF forms via 
>>> a 'PrinterJob' with form field contents.
>>> 
>>> 
>>> -- Related posts --
>>> 
>>> There are several posts or bug reports regard empty form fields, 
>>> like
>> these:
>>> 
>>> *
>>> http://stackoverflow.com/questions/14541647/pdfbox-set-visible-when-
>>> p r inting (not providing a valid solution in my case)
>>> *
>>> http://stackoverflow.com/questions/24149361/form-field-values-set-wi
>>> t h -pdfbo x-not-visible-in-adobe-reader (my PDF form is a AcroForm 
>>> and has no XFA
>>> data)
>>> * https://issues.apache.org/jira/browse/PDFBOX-71 (setting 
>>> 'pdAcroForm.getCOSObject().setItem( COSName.getPDFName( 
>>> "NeedAppearances" ), COSBoolean.TRUE );' did not help in my case)
>>> 
>>> Unfortunately, none of these did help.
>>> 
>>> 
>>> 
>>> Any advice on this problem is highly appreciated
>>> 
>>> 
>>> 
>>> Thanks a lot for your help!
>>> 
>>> 
>>> Best regards,
>>> Timo
>>> 
>>> 
>>> 
>>> --------------------------------------------------------------------
>>> - 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
> 
> 
> 
> ---------------------------------------------------------------------
> 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: After using 'PrinterJob' to print a PDF form, the form fields are empty (PDFBox 2.0.0)

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

> Am 18.05.2016 um 12:13 schrieb Timo Rumland <ti...@digitalys.de>:
> 
> Hello Maruan,
> 
>> unfortunately you are hit by
> https://issues.apache.org/jira/browse/PDFBOX-3332.
>> Will check its state.
>> Maruan
> 
> thanks a lot!
> 
> In the meantime, is there any known workaround? I think about the following:
> 
> 
> I read in the JIRA ticket, that using the standard PDF font Helvetica does
> not cause these problems.
> If this is correct:
> 
> I can't control the source of the PDF files, but the used font for the form
> fields does not matter in my case. So while I can't force the source PDF
> form to use a specific font, I can modify the used font in the PDF form via
> PDFBox (if possible) before printing.
> 
> Is there a way to set the default Helvetica font (specified by
> 'PDType1Font.HELVETICA' as far as I know) for all form fields before
> printing? Maybe this is a solution / workaround in my case, so no weird
> characters are displayed?


you can change your code like this to use Helvetica.

        pdAcroForm.setNeedAppearances(false);
        // Setting PDF field content in "textfield01"
        PDField pdField = pdAcroForm.getField( "textfield01" );
        PDType1Font helv = PDType1Font.HELVETICA;
        // add the font to the AcroForm Resources
        pdAcroForm.getDefaultResources().put(COSName.getPDFName("Helv"), helv);
        // Set the default appearance to use that font.
        // Note: this was harcoded from the original value of 
        // 0 0 0 rg /F2 12 Tf
        // which is not totally correct as the font should be the first parameter
        ((PDTextField)pdField).setDefaultAppearance("/Helv 12 Tf");
        pdField.setValue( "test content" );
        pdAcroForm.setNeedAppearances(true);

BR
Maruan

> 
> 
> Thanks again!
> 
> 
> Best Regards,
> Timo
> 
> 
> 
> 
> 
> 
> 
> 
> -----Ursprüngliche Nachricht-----
> Von: Maruan Sahyoun [mailto:sahyoun@fileaffairs.de] 
> Gesendet: Mittwoch, 18. Mai 2016 10:43
> An: users@pdfbox.apache.org
> Betreff: Re: After using 'PrinterJob' to print a PDF form, the form fields
> are empty (PDFBox 2.0.0)
> 
> Hi,
> 
>> Am 18.05.2016 um 09:14 schrieb Timo Rumland <ti...@digitalys.de>:
>> 
>> Hi Maruan,
>> 
>>> What you can do is set NeedApparances to false, update the field and 
>>> afterwards set the value to true again.
>> 
>> I've just tested this with the following code (I hope I understood you 
>> correctly and my naive approach is sufficient):
>> 
>> ----------
>> 
>> pdAcroForm.setNeedAppearances( false ); pdField.setValue( "test 
>> content" ); pdAcroForm.setNeedAppearances( true );
>> 
>> ----------
>> 
>> (The rest of my sample code is unaltered.)
>> 
>> Now after running the test code, the form field in the resulting PDF 
>> file is NOT empty, but the space between "test" and "content" (the 
>> desired field content is "test content") has been replaced with a 
>> special character (and the position of the text is slightly off).
>> 
>> Please, see this screenshot I made:
>> 
>> * https://goo.gl/dWUvKy
>> 
>> There you can see the difference between the source PDF form file and 
>> the resulting file after printing.
>> 
> 
> unfortunately you are hit by
> https://issues.apache.org/jira/browse/PDFBOX-3332.
> 
> Will check its state.
> Maruan
> 
> 
>> 
>> To be honest, I now have absolutely no idea what could cause this, and 
>> I really hope you have some more ideas about it, as the problem gets 
>> more and more urgent for my current project.
>> 
>> 
>> 
>> Thanks a lot for your help!
>> 
>> 
>> 
>> Best Regards,
>> Timo
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> -----Ursprüngliche Nachricht-----
>> Von: Maruan Sahyoun [mailto:sahyoun@fileaffairs.de]
>> Gesendet: Dienstag, 17. Mai 2016 21:19
>> An: users@pdfbox.apache.org
>> Betreff: Re: After using 'PrinterJob' to print a PDF form, the form 
>> fields are empty (PDFBox 2.0.0)
>> 
>> Hi,
>> 
>>> Am 17.05.2016 um 17:04 schrieb Timo Rumland <ti...@digitalys.de>:
>>> 
>>> Hello,
>>> 
>>> after migrating from PDFBox 1.8 to 2.0.0, printing a PDF form via 
>>> 'PrinterJob' and ' PDFPrintable ', in which I filled out the form 
>>> fields via PDFBox, results in a PDF file (or a physical print) with 
>>> the form fields empty.
>>> 
>> 
>> the form fields value is set but the appearance stream for the form 
>> field (what you see) is not updated as the NeedAppearances flag is set to
> true.
>> PDFBox (currently) handles it that it doens't update the appearance  
>> stream of the form field in that case (that should probably be changed).
>> 
>> What you can do is set NeedApparances to false, update the field and 
>> afterwards set the value to true again.
>> 
>> 
>> BR
>> Maruan
>> 
>> 
>>> With PDFBox 1.8, the following simple code worked:
>>> 
>>> 
>>> ----------
>>> 
>>> File sourcePdfForm = new File( "g:/test-form.pdf" ); File 
>>> resultPdfForm = new File( "g:/test-form_out.pdf" );
>>> 
>>> // Load the source PDF form file
>>> PDDocument pdDocument = PDDocument.load( sourcePdfForm ); 
>>> PDDocumentCatalog pdDocumentCatalog = 
>>> pdDocument.getDocumentCatalog(); PDAcroForm pdAcroForm = 
>>> pdDocumentCatalog.getAcroForm();
>>> 
>>> // Setting PDF field content in "textfield01"
>>> PDField pdField = pdAcroForm.getField( "textfield01" ); 
>>> pdField.setValue( "test content" );
>>> 
>>> pdDocument.save( resultPdfForm );
>>> pdDocument.close();
>>> 
>>> // Open the resulting PDF file, which shows the content in the PDF 
>>> field "textfield01"
>>> Desktop.getDesktop().open( resultPdfForm );
>>> 
>>> 
>>> // Now open the resulting file again, printing it with the default 
>>> printer // No matter if the default printer is an PDF printer (like
>>> PDFCreator) or // an actual physical printer, the content of the 
>>> field "textfield01" is empty
>>> 
>>> pdDocument = PDDocument.load( resultPdfForm );
>>> 
>>> PrinterJob printerJob = PrinterJob.getPrinterJob(); 
>>> printerJob.setJobName( "printerJob" ); printerJob.setPrintService(
>>> PrintServiceLookup.lookupDefaultPrintService()
>>> );   
>>> printerJob.setPrintable( new PDFPrintable( pdDocument ) ); 
>>> printerJob.print( new HashPrintRequestAttributeSet(
>>> MediaSizeName.ISO_A4 ) );
>>> 
>>> pdDocument.close();
>>> 
>>> ----------
>>> 
>>> -- What this code sample does --
>>> 
>>> As you can see, this is a test case where I first load a source PDF 
>>> form, setting the value of a form field and save the resulting PDF 
>>> file. After that I load the resulting PDF file and use a PrinterJob 
>>> to print it on the default printer.
>>> 
>>> 
>>> -- The problem --
>>> 
>>> Now using PDFBox 2.0.0: No matter what the default printer is - a PDF 
>>> printer like PDFCreator or a physical printer - the form fields of 
>>> the printed out PDF file are empty
>>> 
>>> Interestingly, opening the resulting PDF form 'test-form_out.pdf' 
>>> (see line 'Desktop.getDesktop().open( resultPdfForm );') in any PDF 
>>> viewer (Adobe Acrobat Reader DC, PDF XChange Viewer etc.) shows the 
>>> correct form field content for field 'textfield01', which is 'test
> content'.
>>> 
>>> The test PDF form file I use is a really simple form, containing only 
>>> a label and the form field named "testfield01". If you wish, you can 
>>> download 'test-form.pdf' here:
>>> 
>>> * https://goo.gl/VMiohj
>>> 
>>> As mentioned, with PDFBox 1.8 I was able to print out PDF forms via a 
>>> 'PrinterJob' with form field contents.
>>> 
>>> 
>>> -- Related posts --
>>> 
>>> There are several posts or bug reports regard empty form fields, like
>> these:
>>> 
>>> *
>>> http://stackoverflow.com/questions/14541647/pdfbox-set-visible-when-p
>>> r inting (not providing a valid solution in my case)
>>> *
>>> http://stackoverflow.com/questions/24149361/form-field-values-set-wit
>>> h -pdfbo x-not-visible-in-adobe-reader (my PDF form is a AcroForm and 
>>> has no XFA
>>> data)
>>> * https://issues.apache.org/jira/browse/PDFBOX-71 (setting 
>>> 'pdAcroForm.getCOSObject().setItem( COSName.getPDFName( 
>>> "NeedAppearances" ), COSBoolean.TRUE );' did not help in my case)
>>> 
>>> Unfortunately, none of these did help.
>>> 
>>> 
>>> 
>>> Any advice on this problem is highly appreciated
>>> 
>>> 
>>> 
>>> Thanks a lot for your help!
>>> 
>>> 
>>> Best regards,
>>> Timo
>>> 
>>> 
>>> 
>>> ---------------------------------------------------------------------
>>> 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
> 
> 
> 
> ---------------------------------------------------------------------
> 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: After using 'PrinterJob' to print a PDF form, the form fields are empty (PDFBox 2.0.0)

Posted by Timo Rumland <ti...@digitalys.de>.
Hello Maruan,

> unfortunately you are hit by
https://issues.apache.org/jira/browse/PDFBOX-3332.
> Will check its state.
> Maruan

thanks a lot!

In the meantime, is there any known workaround? I think about the following:


I read in the JIRA ticket, that using the standard PDF font Helvetica does
not cause these problems.
If this is correct:

I can't control the source of the PDF files, but the used font for the form
fields does not matter in my case. So while I can't force the source PDF
form to use a specific font, I can modify the used font in the PDF form via
PDFBox (if possible) before printing.

Is there a way to set the default Helvetica font (specified by
'PDType1Font.HELVETICA' as far as I know) for all form fields before
printing? Maybe this is a solution / workaround in my case, so no weird
characters are displayed?


Thanks again!


Best Regards,
Timo








-----Ursprüngliche Nachricht-----
Von: Maruan Sahyoun [mailto:sahyoun@fileaffairs.de] 
Gesendet: Mittwoch, 18. Mai 2016 10:43
An: users@pdfbox.apache.org
Betreff: Re: After using 'PrinterJob' to print a PDF form, the form fields
are empty (PDFBox 2.0.0)

Hi,

> Am 18.05.2016 um 09:14 schrieb Timo Rumland <ti...@digitalys.de>:
> 
> Hi Maruan,
> 
>> What you can do is set NeedApparances to false, update the field and 
>> afterwards set the value to true again.
> 
> I've just tested this with the following code (I hope I understood you 
> correctly and my naive approach is sufficient):
> 
> ----------
> 
> pdAcroForm.setNeedAppearances( false ); pdField.setValue( "test 
> content" ); pdAcroForm.setNeedAppearances( true );
> 
> ----------
> 
> (The rest of my sample code is unaltered.)
> 
> Now after running the test code, the form field in the resulting PDF 
> file is NOT empty, but the space between "test" and "content" (the 
> desired field content is "test content") has been replaced with a 
> special character (and the position of the text is slightly off).
> 
> Please, see this screenshot I made:
> 
> * https://goo.gl/dWUvKy
> 
> There you can see the difference between the source PDF form file and 
> the resulting file after printing.
> 

unfortunately you are hit by
https://issues.apache.org/jira/browse/PDFBOX-3332.

Will check its state.
Maruan


> 
> To be honest, I now have absolutely no idea what could cause this, and 
> I really hope you have some more ideas about it, as the problem gets 
> more and more urgent for my current project.
> 
> 
> 
> Thanks a lot for your help!
> 
> 
> 
> Best Regards,
> Timo
> 
> 
> 
> 
> 
> 
> 
> -----Ursprüngliche Nachricht-----
> Von: Maruan Sahyoun [mailto:sahyoun@fileaffairs.de]
> Gesendet: Dienstag, 17. Mai 2016 21:19
> An: users@pdfbox.apache.org
> Betreff: Re: After using 'PrinterJob' to print a PDF form, the form 
> fields are empty (PDFBox 2.0.0)
> 
> Hi,
> 
>> Am 17.05.2016 um 17:04 schrieb Timo Rumland <ti...@digitalys.de>:
>> 
>> Hello,
>> 
>> after migrating from PDFBox 1.8 to 2.0.0, printing a PDF form via 
>> 'PrinterJob' and ' PDFPrintable ', in which I filled out the form 
>> fields via PDFBox, results in a PDF file (or a physical print) with 
>> the form fields empty.
>> 
> 
> the form fields value is set but the appearance stream for the form 
> field (what you see) is not updated as the NeedAppearances flag is set to
true.
> PDFBox (currently) handles it that it doens't update the appearance  
> stream of the form field in that case (that should probably be changed).
> 
> What you can do is set NeedApparances to false, update the field and 
> afterwards set the value to true again.
> 
> 
> BR
> Maruan
> 
> 
>> With PDFBox 1.8, the following simple code worked:
>> 
>> 
>> ----------
>> 
>> File sourcePdfForm = new File( "g:/test-form.pdf" ); File 
>> resultPdfForm = new File( "g:/test-form_out.pdf" );
>> 
>> // Load the source PDF form file
>> PDDocument pdDocument = PDDocument.load( sourcePdfForm ); 
>> PDDocumentCatalog pdDocumentCatalog = 
>> pdDocument.getDocumentCatalog(); PDAcroForm pdAcroForm = 
>> pdDocumentCatalog.getAcroForm();
>> 
>> // Setting PDF field content in "textfield01"
>> PDField pdField = pdAcroForm.getField( "textfield01" ); 
>> pdField.setValue( "test content" );
>> 
>> pdDocument.save( resultPdfForm );
>> pdDocument.close();
>> 
>> // Open the resulting PDF file, which shows the content in the PDF 
>> field "textfield01"
>> Desktop.getDesktop().open( resultPdfForm );
>> 
>> 
>> // Now open the resulting file again, printing it with the default 
>> printer // No matter if the default printer is an PDF printer (like
>> PDFCreator) or // an actual physical printer, the content of the 
>> field "textfield01" is empty
>> 
>> pdDocument = PDDocument.load( resultPdfForm );
>> 
>> PrinterJob printerJob = PrinterJob.getPrinterJob(); 
>> printerJob.setJobName( "printerJob" ); printerJob.setPrintService(
>> PrintServiceLookup.lookupDefaultPrintService()
>> );   
>> printerJob.setPrintable( new PDFPrintable( pdDocument ) ); 
>> printerJob.print( new HashPrintRequestAttributeSet(
>> MediaSizeName.ISO_A4 ) );
>> 
>> pdDocument.close();
>> 
>> ----------
>> 
>> -- What this code sample does --
>> 
>> As you can see, this is a test case where I first load a source PDF 
>> form, setting the value of a form field and save the resulting PDF 
>> file. After that I load the resulting PDF file and use a PrinterJob 
>> to print it on the default printer.
>> 
>> 
>> -- The problem --
>> 
>> Now using PDFBox 2.0.0: No matter what the default printer is - a PDF 
>> printer like PDFCreator or a physical printer - the form fields of 
>> the printed out PDF file are empty
>> 
>> Interestingly, opening the resulting PDF form 'test-form_out.pdf' 
>> (see line 'Desktop.getDesktop().open( resultPdfForm );') in any PDF 
>> viewer (Adobe Acrobat Reader DC, PDF XChange Viewer etc.) shows the 
>> correct form field content for field 'textfield01', which is 'test
content'.
>> 
>> The test PDF form file I use is a really simple form, containing only 
>> a label and the form field named "testfield01". If you wish, you can 
>> download 'test-form.pdf' here:
>> 
>> * https://goo.gl/VMiohj
>> 
>> As mentioned, with PDFBox 1.8 I was able to print out PDF forms via a 
>> 'PrinterJob' with form field contents.
>> 
>> 
>> -- Related posts --
>> 
>> There are several posts or bug reports regard empty form fields, like
> these:
>> 
>> *
>> http://stackoverflow.com/questions/14541647/pdfbox-set-visible-when-p
>> r inting (not providing a valid solution in my case)
>> *
>> http://stackoverflow.com/questions/24149361/form-field-values-set-wit
>> h -pdfbo x-not-visible-in-adobe-reader (my PDF form is a AcroForm and 
>> has no XFA
>> data)
>> * https://issues.apache.org/jira/browse/PDFBOX-71 (setting 
>> 'pdAcroForm.getCOSObject().setItem( COSName.getPDFName( 
>> "NeedAppearances" ), COSBoolean.TRUE );' did not help in my case)
>> 
>> Unfortunately, none of these did help.
>> 
>> 
>> 
>> Any advice on this problem is highly appreciated
>> 
>> 
>> 
>> Thanks a lot for your help!
>> 
>> 
>> Best regards,
>> Timo
>> 
>> 
>> 
>> ---------------------------------------------------------------------
>> 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



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


Re: After using 'PrinterJob' to print a PDF form, the form fields are empty (PDFBox 2.0.0)

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

> Am 18.05.2016 um 09:14 schrieb Timo Rumland <ti...@digitalys.de>:
> 
> Hi Maruan,
> 
>> What you can do is set NeedApparances to false, update 
>> the field and afterwards set the value to true again.
> 
> I've just tested this with the following code (I hope I understood you
> correctly and my naive approach is sufficient):
> 
> ----------
> 
> pdAcroForm.setNeedAppearances( false );
> pdField.setValue( "test content" );
> pdAcroForm.setNeedAppearances( true );
> 
> ----------
> 
> (The rest of my sample code is unaltered.)
> 
> Now after running the test code, the form field in the resulting PDF file is
> NOT empty, but the space between "test" and "content" (the desired field
> content is "test content") has been replaced with a special character (and
> the position of the text is slightly off).
> 
> Please, see this screenshot I made:
> 
> * https://goo.gl/dWUvKy
> 
> There you can see the difference between the source PDF form file and the
> resulting file after printing.
> 

unfortunately you are hit by https://issues.apache.org/jira/browse/PDFBOX-3332.

Will check its state.
Maruan


> 
> To be honest, I now have absolutely no idea what could cause this, and I
> really hope you have some more ideas about it, as the problem gets more and
> more urgent for my current project.
> 
> 
> 
> Thanks a lot for your help!
> 
> 
> 
> Best Regards,
> Timo
> 
> 
> 
> 
> 
> 
> 
> -----Ursprüngliche Nachricht-----
> Von: Maruan Sahyoun [mailto:sahyoun@fileaffairs.de] 
> Gesendet: Dienstag, 17. Mai 2016 21:19
> An: users@pdfbox.apache.org
> Betreff: Re: After using 'PrinterJob' to print a PDF form, the form fields
> are empty (PDFBox 2.0.0)
> 
> Hi,
> 
>> Am 17.05.2016 um 17:04 schrieb Timo Rumland <ti...@digitalys.de>:
>> 
>> Hello,
>> 
>> after migrating from PDFBox 1.8 to 2.0.0, printing a PDF form via 
>> 'PrinterJob' and ' PDFPrintable ', in which I filled out the form 
>> fields via PDFBox, results in a PDF file (or a physical print) with 
>> the form fields empty.
>> 
> 
> the form fields value is set but the appearance stream for the form field
> (what you see) is not updated as the NeedAppearances flag is set to true.
> PDFBox (currently) handles it that it doens't update the appearance  stream
> of the form field in that case (that should probably be changed).
> 
> What you can do is set NeedApparances to false, update the field and
> afterwards set the value to true again.
> 
> 
> BR
> Maruan
> 
> 
>> With PDFBox 1.8, the following simple code worked:
>> 
>> 
>> ----------
>> 
>> File sourcePdfForm = new File( "g:/test-form.pdf" ); File 
>> resultPdfForm = new File( "g:/test-form_out.pdf" );
>> 
>> // Load the source PDF form file
>> PDDocument pdDocument = PDDocument.load( sourcePdfForm ); 
>> PDDocumentCatalog pdDocumentCatalog = pdDocument.getDocumentCatalog(); 
>> PDAcroForm pdAcroForm = pdDocumentCatalog.getAcroForm();
>> 
>> // Setting PDF field content in "textfield01"
>> PDField pdField = pdAcroForm.getField( "textfield01" ); 
>> pdField.setValue( "test content" );
>> 
>> pdDocument.save( resultPdfForm );
>> pdDocument.close();
>> 
>> // Open the resulting PDF file, which shows the content in the PDF 
>> field "textfield01"
>> Desktop.getDesktop().open( resultPdfForm );
>> 
>> 
>> // Now open the resulting file again, printing it with the default 
>> printer // No matter if the default printer is an PDF printer (like 
>> PDFCreator) or // an actual physical printer, the content of the field 
>> "textfield01" is empty
>> 
>> pdDocument = PDDocument.load( resultPdfForm );
>> 
>> PrinterJob printerJob = PrinterJob.getPrinterJob(); 
>> printerJob.setJobName( "printerJob" ); printerJob.setPrintService( 
>> PrintServiceLookup.lookupDefaultPrintService()
>> );   
>> printerJob.setPrintable( new PDFPrintable( pdDocument ) ); 
>> printerJob.print( new HashPrintRequestAttributeSet( 
>> MediaSizeName.ISO_A4 ) );
>> 
>> pdDocument.close();
>> 
>> ----------
>> 
>> -- What this code sample does --
>> 
>> As you can see, this is a test case where I first load a source PDF 
>> form, setting the value of a form field and save the resulting PDF 
>> file. After that I load the resulting PDF file and use a PrinterJob to 
>> print it on the default printer.
>> 
>> 
>> -- The problem --
>> 
>> Now using PDFBox 2.0.0: No matter what the default printer is - a PDF 
>> printer like PDFCreator or a physical printer - the form fields of the 
>> printed out PDF file are empty
>> 
>> Interestingly, opening the resulting PDF form 'test-form_out.pdf' (see 
>> line 'Desktop.getDesktop().open( resultPdfForm );') in any PDF viewer 
>> (Adobe Acrobat Reader DC, PDF XChange Viewer etc.) shows the correct 
>> form field content for field 'textfield01', which is 'test content'.
>> 
>> The test PDF form file I use is a really simple form, containing only 
>> a label and the form field named "testfield01". If you wish, you can 
>> download 'test-form.pdf' here:
>> 
>> * https://goo.gl/VMiohj
>> 
>> As mentioned, with PDFBox 1.8 I was able to print out PDF forms via a 
>> 'PrinterJob' with form field contents.
>> 
>> 
>> -- Related posts --
>> 
>> There are several posts or bug reports regard empty form fields, like
> these:
>> 
>> *
>> http://stackoverflow.com/questions/14541647/pdfbox-set-visible-when-pr
>> inting (not providing a valid solution in my case)
>> *
>> http://stackoverflow.com/questions/24149361/form-field-values-set-with
>> -pdfbo x-not-visible-in-adobe-reader (my PDF form is a AcroForm and 
>> has no XFA
>> data)
>> * https://issues.apache.org/jira/browse/PDFBOX-71 (setting 
>> 'pdAcroForm.getCOSObject().setItem( COSName.getPDFName( 
>> "NeedAppearances" ), COSBoolean.TRUE );' did not help in my case)
>> 
>> Unfortunately, none of these did help.
>> 
>> 
>> 
>> Any advice on this problem is highly appreciated
>> 
>> 
>> 
>> Thanks a lot for your help!
>> 
>> 
>> Best regards,
>> Timo
>> 
>> 
>> 
>> ---------------------------------------------------------------------
>> 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


AW: After using 'PrinterJob' to print a PDF form, the form fields are empty (PDFBox 2.0.0)

Posted by Timo Rumland <ti...@digitalys.de>.
Hi Maruan,

> What you can do is set NeedApparances to false, update 
> the field and afterwards set the value to true again.

I've just tested this with the following code (I hope I understood you
correctly and my naive approach is sufficient):

----------

pdAcroForm.setNeedAppearances( false );
pdField.setValue( "test content" );
pdAcroForm.setNeedAppearances( true );

----------

(The rest of my sample code is unaltered.)

Now after running the test code, the form field in the resulting PDF file is
NOT empty, but the space between "test" and "content" (the desired field
content is "test content") has been replaced with a special character (and
the position of the text is slightly off).

Please, see this screenshot I made:

 * https://goo.gl/dWUvKy

There you can see the difference between the source PDF form file and the
resulting file after printing.


To be honest, I now have absolutely no idea what could cause this, and I
really hope you have some more ideas about it, as the problem gets more and
more urgent for my current project.



Thanks a lot for your help!



Best Regards,
Timo







-----Ursprüngliche Nachricht-----
Von: Maruan Sahyoun [mailto:sahyoun@fileaffairs.de] 
Gesendet: Dienstag, 17. Mai 2016 21:19
An: users@pdfbox.apache.org
Betreff: Re: After using 'PrinterJob' to print a PDF form, the form fields
are empty (PDFBox 2.0.0)

Hi,

> Am 17.05.2016 um 17:04 schrieb Timo Rumland <ti...@digitalys.de>:
> 
> Hello,
> 
> after migrating from PDFBox 1.8 to 2.0.0, printing a PDF form via 
> 'PrinterJob' and ' PDFPrintable ', in which I filled out the form 
> fields via PDFBox, results in a PDF file (or a physical print) with 
> the form fields empty.
> 

the form fields value is set but the appearance stream for the form field
(what you see) is not updated as the NeedAppearances flag is set to true.
PDFBox (currently) handles it that it doens't update the appearance  stream
of the form field in that case (that should probably be changed).

What you can do is set NeedApparances to false, update the field and
afterwards set the value to true again.


BR
Maruan


> With PDFBox 1.8, the following simple code worked:
> 
> 
> ----------
> 
> File sourcePdfForm = new File( "g:/test-form.pdf" ); File 
> resultPdfForm = new File( "g:/test-form_out.pdf" );
> 
> // Load the source PDF form file
> PDDocument pdDocument = PDDocument.load( sourcePdfForm ); 
> PDDocumentCatalog pdDocumentCatalog = pdDocument.getDocumentCatalog(); 
> PDAcroForm pdAcroForm = pdDocumentCatalog.getAcroForm();
> 
> // Setting PDF field content in "textfield01"
> PDField pdField = pdAcroForm.getField( "textfield01" ); 
> pdField.setValue( "test content" );
> 
> pdDocument.save( resultPdfForm );
> pdDocument.close();
> 
> // Open the resulting PDF file, which shows the content in the PDF 
> field "textfield01"
> Desktop.getDesktop().open( resultPdfForm );
> 
> 
> // Now open the resulting file again, printing it with the default 
> printer // No matter if the default printer is an PDF printer (like 
> PDFCreator) or // an actual physical printer, the content of the field 
> "textfield01" is empty
> 
> pdDocument = PDDocument.load( resultPdfForm );
> 
> PrinterJob printerJob = PrinterJob.getPrinterJob(); 
> printerJob.setJobName( "printerJob" ); printerJob.setPrintService( 
> PrintServiceLookup.lookupDefaultPrintService()
> );   
> printerJob.setPrintable( new PDFPrintable( pdDocument ) ); 
> printerJob.print( new HashPrintRequestAttributeSet( 
> MediaSizeName.ISO_A4 ) );
> 
> pdDocument.close();
> 
> ----------
> 
> -- What this code sample does --
> 
> As you can see, this is a test case where I first load a source PDF 
> form, setting the value of a form field and save the resulting PDF 
> file. After that I load the resulting PDF file and use a PrinterJob to 
> print it on the default printer.
> 
> 
> -- The problem --
> 
> Now using PDFBox 2.0.0: No matter what the default printer is - a PDF 
> printer like PDFCreator or a physical printer - the form fields of the 
> printed out PDF file are empty
> 
> Interestingly, opening the resulting PDF form 'test-form_out.pdf' (see 
> line 'Desktop.getDesktop().open( resultPdfForm );') in any PDF viewer 
> (Adobe Acrobat Reader DC, PDF XChange Viewer etc.) shows the correct 
> form field content for field 'textfield01', which is 'test content'.
> 
> The test PDF form file I use is a really simple form, containing only 
> a label and the form field named "testfield01". If you wish, you can 
> download 'test-form.pdf' here:
> 
> * https://goo.gl/VMiohj
> 
> As mentioned, with PDFBox 1.8 I was able to print out PDF forms via a 
> 'PrinterJob' with form field contents.
> 
> 
> -- Related posts --
> 
> There are several posts or bug reports regard empty form fields, like
these:
> 
> *
> http://stackoverflow.com/questions/14541647/pdfbox-set-visible-when-pr
> inting (not providing a valid solution in my case)
> *
> http://stackoverflow.com/questions/24149361/form-field-values-set-with
> -pdfbo x-not-visible-in-adobe-reader (my PDF form is a AcroForm and 
> has no XFA
> data)
> * https://issues.apache.org/jira/browse/PDFBOX-71 (setting 
> 'pdAcroForm.getCOSObject().setItem( COSName.getPDFName( 
> "NeedAppearances" ), COSBoolean.TRUE );' did not help in my case)
> 
> Unfortunately, none of these did help.
> 
> 
> 
> Any advice on this problem is highly appreciated
> 
> 
> 
> Thanks a lot for your help!
> 
> 
> Best regards,
> Timo
> 
> 
> 
> ---------------------------------------------------------------------
> 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: After using 'PrinterJob' to print a PDF form, the form fields are empty (PDFBox 2.0.0)

Posted by John Hewson <jo...@jahewson.com>.
> On 17 May 2016, at 12:18, Maruan Sahyoun <sa...@fileaffairs.de> wrote:
> 
> Hi,
> 
>> Am 17.05.2016 um 17:04 schrieb Timo Rumland <ti...@digitalys.de>:
>> 
>> Hello,
>> 
>> after migrating from PDFBox 1.8 to 2.0.0, printing a PDF form via
>> 'PrinterJob' and ' PDFPrintable ', in which I filled out the form fields via
>> PDFBox, results in a PDF file (or a physical print) with the form fields
>> empty.
>> 
> 
> the form fields value is set but the appearance stream for the form field (what you see) is not updated as the
> NeedAppearances flag is set to true. PDFBox (currently) handles it that it doens't update the appearance  stream
> of the form field in that case (that should probably be changed).

Yeah - it seems like it would be ok for PDFBox to update the appearance stream, even though NeedAppearances is true.

— John

> What you can do is set NeedApparances to false, update the field and afterwards set the value to true again.
> 
> 
> BR
> Maruan
> 
> 
>> With PDFBox 1.8, the following simple code worked:
>> 
>> 
>> ----------
>> 
>> File sourcePdfForm = new File( "g:/test-form.pdf" );
>> File resultPdfForm = new File( "g:/test-form_out.pdf" );
>> 
>> // Load the source PDF form file
>> PDDocument pdDocument = PDDocument.load( sourcePdfForm );
>> PDDocumentCatalog pdDocumentCatalog = pdDocument.getDocumentCatalog();
>> PDAcroForm pdAcroForm = pdDocumentCatalog.getAcroForm();
>> 
>> // Setting PDF field content in "textfield01"
>> PDField pdField = pdAcroForm.getField( "textfield01" );
>> pdField.setValue( "test content" );
>> 
>> pdDocument.save( resultPdfForm );
>> pdDocument.close();
>> 
>> // Open the resulting PDF file, which shows the content in the PDF field
>> "textfield01"
>> Desktop.getDesktop().open( resultPdfForm );
>> 
>> 
>> // Now open the resulting file again, printing it with the default printer
>> // No matter if the default printer is an PDF printer (like PDFCreator) or
>> // an actual physical printer, the content of the field "textfield01" is
>> empty
>> 
>> pdDocument = PDDocument.load( resultPdfForm );
>> 
>> PrinterJob printerJob = PrinterJob.getPrinterJob();
>> printerJob.setJobName( "printerJob" );
>> printerJob.setPrintService( PrintServiceLookup.lookupDefaultPrintService()
>> );   
>> printerJob.setPrintable( new PDFPrintable( pdDocument ) );
>> printerJob.print( new HashPrintRequestAttributeSet( MediaSizeName.ISO_A4 )
>> );
>> 
>> pdDocument.close();
>> 
>> ----------
>> 
>> -- What this code sample does --
>> 
>> As you can see, this is a test case where I first load a source PDF form,
>> setting the value of a form field and save the resulting PDF file. After
>> that I load the resulting PDF file and use a PrinterJob to print it on the
>> default printer.
>> 
>> 
>> -- The problem --
>> 
>> Now using PDFBox 2.0.0: No matter what the default printer is - a PDF
>> printer like PDFCreator or a physical printer - the form fields of the
>> printed out PDF file are empty
>> 
>> Interestingly, opening the resulting PDF form 'test-form_out.pdf' (see line
>> 'Desktop.getDesktop().open( resultPdfForm );') in any PDF viewer (Adobe
>> Acrobat Reader DC, PDF XChange Viewer etc.) shows the correct form field
>> content for field 'textfield01', which is 'test content'.
>> 
>> The test PDF form file I use is a really simple form, containing only a
>> label and the form field named "testfield01". If you wish, you can download
>> 'test-form.pdf' here:
>> 
>> * https://goo.gl/VMiohj
>> 
>> As mentioned, with PDFBox 1.8 I was able to print out PDF forms via a
>> 'PrinterJob' with form field contents.
>> 
>> 
>> -- Related posts --
>> 
>> There are several posts or bug reports regard empty form fields, like these:
>> 
>> *
>> http://stackoverflow.com/questions/14541647/pdfbox-set-visible-when-printing
>> (not providing a valid solution in my case)
>> *
>> http://stackoverflow.com/questions/24149361/form-field-values-set-with-pdfbo
>> x-not-visible-in-adobe-reader (my PDF form is a AcroForm and has no XFA
>> data)
>> * https://issues.apache.org/jira/browse/PDFBOX-71 (setting
>> 'pdAcroForm.getCOSObject().setItem( COSName.getPDFName( "NeedAppearances" ),
>> COSBoolean.TRUE );' did not help in my case)
>> 
>> Unfortunately, none of these did help.
>> 
>> 
>> 
>> Any advice on this problem is highly appreciated
>> 
>> 
>> 
>> Thanks a lot for your help!
>> 
>> 
>> Best regards,
>> Timo
>> 
>> 
>> 
>> ---------------------------------------------------------------------
>> 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: After using 'PrinterJob' to print a PDF form, the form fields are empty (PDFBox 2.0.0)

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

> Am 17.05.2016 um 17:04 schrieb Timo Rumland <ti...@digitalys.de>:
> 
> Hello,
> 
> after migrating from PDFBox 1.8 to 2.0.0, printing a PDF form via
> 'PrinterJob' and ' PDFPrintable ', in which I filled out the form fields via
> PDFBox, results in a PDF file (or a physical print) with the form fields
> empty.
> 

the form fields value is set but the appearance stream for the form field (what you see) is not updated as the
NeedAppearances flag is set to true. PDFBox (currently) handles it that it doens't update the appearance  stream
of the form field in that case (that should probably be changed).

What you can do is set NeedApparances to false, update the field and afterwards set the value to true again.


BR
Maruan


> With PDFBox 1.8, the following simple code worked:
> 
> 
> ----------
> 
> File sourcePdfForm = new File( "g:/test-form.pdf" );
> File resultPdfForm = new File( "g:/test-form_out.pdf" );
> 
> // Load the source PDF form file
> PDDocument pdDocument = PDDocument.load( sourcePdfForm );
> PDDocumentCatalog pdDocumentCatalog = pdDocument.getDocumentCatalog();
> PDAcroForm pdAcroForm = pdDocumentCatalog.getAcroForm();
> 
> // Setting PDF field content in "textfield01"
> PDField pdField = pdAcroForm.getField( "textfield01" );
> pdField.setValue( "test content" );
> 
> pdDocument.save( resultPdfForm );
> pdDocument.close();
> 
> // Open the resulting PDF file, which shows the content in the PDF field
> "textfield01"
> Desktop.getDesktop().open( resultPdfForm );
> 
> 
> // Now open the resulting file again, printing it with the default printer
> // No matter if the default printer is an PDF printer (like PDFCreator) or
> // an actual physical printer, the content of the field "textfield01" is
> empty
> 
> pdDocument = PDDocument.load( resultPdfForm );
> 
> PrinterJob printerJob = PrinterJob.getPrinterJob();
> printerJob.setJobName( "printerJob" );
> printerJob.setPrintService( PrintServiceLookup.lookupDefaultPrintService()
> );   
> printerJob.setPrintable( new PDFPrintable( pdDocument ) );
> printerJob.print( new HashPrintRequestAttributeSet( MediaSizeName.ISO_A4 )
> );
> 
> pdDocument.close();
> 
> ----------
> 
> -- What this code sample does --
> 
> As you can see, this is a test case where I first load a source PDF form,
> setting the value of a form field and save the resulting PDF file. After
> that I load the resulting PDF file and use a PrinterJob to print it on the
> default printer.
> 
> 
> -- The problem --
> 
> Now using PDFBox 2.0.0: No matter what the default printer is - a PDF
> printer like PDFCreator or a physical printer - the form fields of the
> printed out PDF file are empty
> 
> Interestingly, opening the resulting PDF form 'test-form_out.pdf' (see line
> 'Desktop.getDesktop().open( resultPdfForm );') in any PDF viewer (Adobe
> Acrobat Reader DC, PDF XChange Viewer etc.) shows the correct form field
> content for field 'textfield01', which is 'test content'.
> 
> The test PDF form file I use is a really simple form, containing only a
> label and the form field named "testfield01". If you wish, you can download
> 'test-form.pdf' here:
> 
> * https://goo.gl/VMiohj
> 
> As mentioned, with PDFBox 1.8 I was able to print out PDF forms via a
> 'PrinterJob' with form field contents.
> 
> 
> -- Related posts --
> 
> There are several posts or bug reports regard empty form fields, like these:
> 
> *
> http://stackoverflow.com/questions/14541647/pdfbox-set-visible-when-printing
> (not providing a valid solution in my case)
> *
> http://stackoverflow.com/questions/24149361/form-field-values-set-with-pdfbo
> x-not-visible-in-adobe-reader (my PDF form is a AcroForm and has no XFA
> data)
> * https://issues.apache.org/jira/browse/PDFBOX-71 (setting
> 'pdAcroForm.getCOSObject().setItem( COSName.getPDFName( "NeedAppearances" ),
> COSBoolean.TRUE );' did not help in my case)
> 
> Unfortunately, none of these did help.
> 
> 
> 
> Any advice on this problem is highly appreciated
> 
> 
> 
> Thanks a lot for your help!
> 
> 
> Best regards,
> Timo
> 
> 
> 
> ---------------------------------------------------------------------
> 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: After using 'PrinterJob' to print a PDF form, the form fields are empty (PDFBox 2.0.0)

Posted by Timo Rumland <ti...@digitalys.de>.
Hi John,

> Does rendering to an image file work as expected?

I used the following code to render the PDF form to an image...

----------

PDFRenderer pdfRenderer = new PDFRenderer( pdDocument );
BufferedImage bufferedImage = pdfRenderer.renderImage( 0 );
ImageIO.write( bufferedImage, "PNG", new File( "g:/test-form_out.png" ) );

----------

...and the form field is empty, again - exactly the same result, as after printing.


> If not, then it’s a general forms rendering issue, rather than something specific to printing.

Yes, it seems like a rendering problem. I got another answer to my problem from Maruan Sahyoun today and he said, it has something to do with the 'NeedApparances' flag. I will answer him in another e-mail.


Thanks so far for your help!


Best Regards,
Timo





-----Ursprüngliche Nachricht-----
Von: John Hewson [mailto:john@jahewson.com] 
Gesendet: Dienstag, 17. Mai 2016 18:33
An: users@pdfbox.apache.org
Betreff: Re: After using 'PrinterJob' to print a PDF form, the form fields are empty (PDFBox 2.0.0)


> On 17 May 2016, at 08:04, Timo Rumland <ti...@digitalys.de> wrote:
> 
> Hello,
> 
> after migrating from PDFBox 1.8 to 2.0.0, printing a PDF form via 
> 'PrinterJob' and ' PDFPrintable ', in which I filled out the form 
> fields via PDFBox, results in a PDF file (or a physical print) with 
> the form fields empty.
> 
> With PDFBox 1.8, the following simple code worked:
> 
> 
> ----------
> 
> File sourcePdfForm = new File( "g:/test-form.pdf" ); File 
> resultPdfForm = new File( "g:/test-form_out.pdf" );
> 
> // Load the source PDF form file
> PDDocument pdDocument = PDDocument.load( sourcePdfForm ); 
> PDDocumentCatalog pdDocumentCatalog = pdDocument.getDocumentCatalog(); 
> PDAcroForm pdAcroForm = pdDocumentCatalog.getAcroForm();
> 
> // Setting PDF field content in "textfield01"
> PDField pdField = pdAcroForm.getField( "textfield01" ); 
> pdField.setValue( "test content" );
> 
> pdDocument.save( resultPdfForm );
> pdDocument.close();
> 
> // Open the resulting PDF file, which shows the content in the PDF 
> field "textfield01"
> Desktop.getDesktop().open( resultPdfForm );
> 
> 
> // Now open the resulting file again, printing it with the default 
> printer // No matter if the default printer is an PDF printer (like 
> PDFCreator) or // an actual physical printer, the content of the field 
> "textfield01" is empty
> 
> pdDocument = PDDocument.load( resultPdfForm );
> 
> PrinterJob printerJob = PrinterJob.getPrinterJob(); 
> printerJob.setJobName( "printerJob" ); printerJob.setPrintService( 
> PrintServiceLookup.lookupDefaultPrintService()
> );   
> printerJob.setPrintable( new PDFPrintable( pdDocument ) ); 
> printerJob.print( new HashPrintRequestAttributeSet( 
> MediaSizeName.ISO_A4 ) );
> 
> pdDocument.close();
> 
> ----------
> 
> -- What this code sample does --
> 
> As you can see, this is a test case where I first load a source PDF 
> form, setting the value of a form field and save the resulting PDF 
> file. After that I load the resulting PDF file and use a PrinterJob to 
> print it on the default printer.
> 
> 
> -- The problem --
> 
> Now using PDFBox 2.0.0: No matter what the default printer is - a PDF 
> printer like PDFCreator or a physical printer - the form fields of the 
> printed out PDF file are empty
> 
> Interestingly, opening the resulting PDF form 'test-form_out.pdf' (see 
> line 'Desktop.getDesktop().open( resultPdfForm );') in any PDF viewer 
> (Adobe Acrobat Reader DC, PDF XChange Viewer etc.) shows the correct 
> form field content for field 'textfield01', which is 'test content'.
> 
> The test PDF form file I use is a really simple form, containing only 
> a label and the form field named "testfield01". If you wish, you can 
> download 'test-form.pdf' here:
> 
> * https://goo.gl/VMiohj
> 
> As mentioned, with PDFBox 1.8 I was able to print out PDF forms via a 
> 'PrinterJob' with form field contents.
> 
> 
> -- Related posts --
> 
> There are several posts or bug reports regard empty form fields, like these:
> 
> *
> http://stackoverflow.com/questions/14541647/pdfbox-set-visible-when-pr
> inting (not providing a valid solution in my case)
> *
> http://stackoverflow.com/questions/24149361/form-field-values-set-with
> -pdfbo x-not-visible-in-adobe-reader (my PDF form is a AcroForm and 
> has no XFA
> data)
> * https://issues.apache.org/jira/browse/PDFBOX-71 (setting 
> 'pdAcroForm.getCOSObject().setItem( COSName.getPDFName( 
> "NeedAppearances" ), COSBoolean.TRUE );' did not help in my case)
> 
> Unfortunately, none of these did help.
> 
> 
> 
> Any advice on this problem is highly appreciated

Definitely a bug. I don’t know of any obvious reason why printing would encounter this problem, it should be using the same code as rendering. Does rendering to an image file work as expected?

If so, you could print with dpi > 0 as a workaround, which will cause PDFBox to print as a raster image.

If not, then it’s a general forms rendering issue, rather than something specific to printing.

— John

> 
> Thanks a lot for your help!
> 
> 
> Best regards,
> Timo
> 
> 
> 
> ---------------------------------------------------------------------
> 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: After using 'PrinterJob' to print a PDF form, the form fields are empty (PDFBox 2.0.0)

Posted by John Hewson <jo...@jahewson.com>.
> On 17 May 2016, at 08:04, Timo Rumland <ti...@digitalys.de> wrote:
> 
> Hello,
> 
> after migrating from PDFBox 1.8 to 2.0.0, printing a PDF form via
> 'PrinterJob' and ' PDFPrintable ', in which I filled out the form fields via
> PDFBox, results in a PDF file (or a physical print) with the form fields
> empty.
> 
> With PDFBox 1.8, the following simple code worked:
> 
> 
> ----------
> 
> File sourcePdfForm = new File( "g:/test-form.pdf" );
> File resultPdfForm = new File( "g:/test-form_out.pdf" );
> 
> // Load the source PDF form file
> PDDocument pdDocument = PDDocument.load( sourcePdfForm );
> PDDocumentCatalog pdDocumentCatalog = pdDocument.getDocumentCatalog();
> PDAcroForm pdAcroForm = pdDocumentCatalog.getAcroForm();
> 
> // Setting PDF field content in "textfield01"
> PDField pdField = pdAcroForm.getField( "textfield01" );
> pdField.setValue( "test content" );
> 
> pdDocument.save( resultPdfForm );
> pdDocument.close();
> 
> // Open the resulting PDF file, which shows the content in the PDF field
> "textfield01"
> Desktop.getDesktop().open( resultPdfForm );
> 
> 
> // Now open the resulting file again, printing it with the default printer
> // No matter if the default printer is an PDF printer (like PDFCreator) or
> // an actual physical printer, the content of the field "textfield01" is
> empty
> 
> pdDocument = PDDocument.load( resultPdfForm );
> 
> PrinterJob printerJob = PrinterJob.getPrinterJob();
> printerJob.setJobName( "printerJob" );
> printerJob.setPrintService( PrintServiceLookup.lookupDefaultPrintService()
> );   
> printerJob.setPrintable( new PDFPrintable( pdDocument ) );
> printerJob.print( new HashPrintRequestAttributeSet( MediaSizeName.ISO_A4 )
> );
> 
> pdDocument.close();
> 
> ----------
> 
> -- What this code sample does --
> 
> As you can see, this is a test case where I first load a source PDF form,
> setting the value of a form field and save the resulting PDF file. After
> that I load the resulting PDF file and use a PrinterJob to print it on the
> default printer.
> 
> 
> -- The problem --
> 
> Now using PDFBox 2.0.0: No matter what the default printer is - a PDF
> printer like PDFCreator or a physical printer - the form fields of the
> printed out PDF file are empty
> 
> Interestingly, opening the resulting PDF form 'test-form_out.pdf' (see line
> 'Desktop.getDesktop().open( resultPdfForm );') in any PDF viewer (Adobe
> Acrobat Reader DC, PDF XChange Viewer etc.) shows the correct form field
> content for field 'textfield01', which is 'test content'.
> 
> The test PDF form file I use is a really simple form, containing only a
> label and the form field named "testfield01". If you wish, you can download
> 'test-form.pdf' here:
> 
> * https://goo.gl/VMiohj
> 
> As mentioned, with PDFBox 1.8 I was able to print out PDF forms via a
> 'PrinterJob' with form field contents.
> 
> 
> -- Related posts --
> 
> There are several posts or bug reports regard empty form fields, like these:
> 
> *
> http://stackoverflow.com/questions/14541647/pdfbox-set-visible-when-printing
> (not providing a valid solution in my case)
> *
> http://stackoverflow.com/questions/24149361/form-field-values-set-with-pdfbo
> x-not-visible-in-adobe-reader (my PDF form is a AcroForm and has no XFA
> data)
> * https://issues.apache.org/jira/browse/PDFBOX-71 (setting
> 'pdAcroForm.getCOSObject().setItem( COSName.getPDFName( "NeedAppearances" ),
> COSBoolean.TRUE );' did not help in my case)
> 
> Unfortunately, none of these did help.
> 
> 
> 
> Any advice on this problem is highly appreciated

Definitely a bug. I don’t know of any obvious reason why printing would encounter this
problem, it should be using the same code as rendering. Does rendering to an image
file work as expected?

If so, you could print with dpi > 0 as a workaround, which will cause PDFBox to print as
a raster image.

If not, then it’s a general forms rendering issue, rather than something specific to printing.

— John

> 
> Thanks a lot for your help!
> 
> 
> Best regards,
> Timo
> 
> 
> 
> ---------------------------------------------------------------------
> 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