Re "bigger each time it's opened and then saved", hard to tell without
the PDF. Maybe the font you're replacing is used elsewhere due to other
code that used this font.
If you can't share the PDF, look at it with PDFDebugger or with
NOTEPAD++. In PDFDebugger don't forget to switch between the page view
and the internal structure view (in the view menu)
Tilman
On 26.04.2024 15:50, Jurgen Doll wrote:
Hi Tilman
My bad, I was working on the image code and just assumed that it was
the culprit.
It turns out that I must be embedding fonts incorrectly. I use this
code each time I open the PDF:
var resources = Objects.requireNonNullElseGet(
pdForm.getDefaultResources(), PDResources::new );
var lucidaFont = PDType0Font.load( pdfDoc,
LUCIDA_UNICODE_FILE.get() );
var cosName = COSName.getPDFName( lucidaFont.getName() );
resources.put( cosName, lucidaFont );
pdForm.setDefaultResources( resources );
The above seems to make the PDF get bigger each time it's opened and
then saved.
So I've added a check first, like this:
if ( resources.getFont( cosName ) == null )
{
resources.put( cosName, lucidaFont );
pdForm.setDefaultResources( resources );
}
And now the PDF doesn't seem to be growing any more.
Thanks,
Jurgen
On Fri, 26 Apr 2024 14:58:26 +0200, Tilman Hausherr
<thaush...@t-online.de> wrote:
Do you save directly or incrementally?
If directly then the old one should be gone. If not, please share the
PDF (upload to sharehoster) and tell us why you think it's still there.
Tilman
On 26.04.2024 12:37, Jurgen Doll wrote:
Hi
I would like to know how to remove an image resource from a PDF form.
I use the following code to set an image object on a field:
private static void setAppearance( PDDocument pdfDoc, PDField
field, PDImageXObject image )
{
var widget = field.getWidgets().get(0);
var rect = widget.getRectangle();
var formObj = new PDFormXObject( new PDStream( pdfDoc ) );
formObj.setResources( new PDResources() );
formObj.setBBox( new PDRectangle
(
// The TransformationMatrix below enlarges the image
slightly,
// so adjust accordingly here otherwise the image is
cropped.
rect.getLowerLeftX(), rect.getLowerLeftY() - 10,
rect.getWidth(), rect.getHeight() + 20
));
formObj.setFormType(1);
var appearanceStream = new PDAppearanceStream(
formObj.getCOSObject() );
var pageContentStream = new PDPageContentStream( pdfDoc,
appearanceStream );
pageContentStream.drawImage( image, getTransformationMatrix(
image, widget ) );
pageContentStream.close();
var appearance = new PDAppearanceDictionary();
appearance.setNormalAppearance( appearanceStream );
appearance.getCOSObject().setDirect( true );
widget.setAppearance( appearance );
}
To remove the image I do "widget.setAppearance( null )" or to
replace the image I use the above method to set the new image as the
appearance. However I've noticed that this doesn't remove the
previous image object/resource from the PDF. How do I go about
removing it ?
Thanks, regards
Jurgen
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: users-h...@pdfbox.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: users-h...@pdfbox.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: users-h...@pdfbox.apache.org