[ https://issues.apache.org/jira/browse/PDFBOX-5343 ]
Mohamed Sameem deleted comment on PDFBOX-5343:
----------------------------------------
was (Author: JIRAUSER281940):
Yeah by default its a button, so i had to implement that way, i could share
yout the generic piece of code where i do , the thing is image we paste
isn't persistent after flattening.
The below is the script, ping me if u find anything wrong over here.
{code}
PDNonTerminalField imageBoxes = (PDNonTerminalField) pdAcroForm.getField("#i");
if (Objects.nonNull(imageBoxes)) {
imageBoxes
.getChildren()
.forEach(
x -> {
PDPushButton pdNonTerminalFieldX = (PDPushButton) x;
setField(
pdNonTerminalFieldX,
values.get(pdNonTerminalFieldX.getPartialName()),
pdfDocument);
});
}
for (PDField field : pdAcroForm.getFieldTree()) {
field.setReadOnly(true);
}
pdfDocument.save(byteArrayOutputStream);
pdfDocument.close();
return byteArrayOutputStream.toByteArray();
} catch (Exception e) {
log.error("exception caught while generating pdf: ", e);
return null;
}
}
private static PDRectangle getFieldArea(PDAnnotationWidget
pdAnnotationWidget) {
COSDictionary fieldDict = pdAnnotationWidget.getCOSObject();
COSArray fieldAreaArray = (COSArray)
fieldDict.getDictionaryObject(COSName.RECT);
return new PDRectangle(fieldAreaArray);
}
private static void setField(PDField field, String value, PDDocument
pdfDocument) {
if (Objects.isNull(field)) {
return;
}
try {
if (field instanceof PDCheckBox) {
PDCheckBox checkbox = (PDCheckBox) field;
if (value.isEmpty()) {
checkbox.unCheck();
} else {
checkbox.check();
}
} else if (field instanceof PDComboBox) {
field.setValue(value);
} else if (field instanceof PDListBox) {
field.setValue(value);
} else if (field instanceof PDRadioButton) {
field.setValue(value);
} else if (field instanceof PDTextField) {
PDTextField textField = (PDTextField) field;
textField.setDefaultAppearance("/Helv 22 Tf 0 g");
textField.setValue(value);
} else if (field instanceof PDPushButton) {
setImageField(field, value, pdfDocument);
}
} catch (Exception e) {
log.error("Failed to set value {} at field {}", value,
field.getFullyQualifiedName(), e);
}
}
static void setImageField(PDField field, String value, PDDocument pdfDocument)
throws IOException {
if (StringUtils.isEmpty(value)) {
return;
}
PDPushButton pdPushButton = (PDPushButton) field;
List<PDAnnotationWidget> widgets = pdPushButton.getWidgets();
byte[] byteArray = Base64.getDecoder().decode(value);
for (PDAnnotationWidget annotationWidget : widgets) {
PDImageXObject pdImageXObject =
PDImageXObject.createFromByteArray(pdfDocument, byteArray,
field.getPartialName());
float imageScaleRatio =
(float) pdImageXObject.getHeight() / (float)
pdImageXObject.getWidth();
PDRectangle buttonPosition = getFieldArea(annotationWidget);
float height = buttonPosition.getHeight();
float width = height / imageScaleRatio;
float x = buttonPosition.getLowerLeftX();
float y = buttonPosition.getLowerLeftY();
PDAppearanceStream pdAppearanceStream = new
PDAppearanceStream(pdfDocument);
pdAppearanceStream.setResources(new PDResources());
try (PDPageContentStream pdPageContentStream =
new PDPageContentStream(pdfDocument, pdAppearanceStream)) {
pdPageContentStream.drawImage(pdImageXObject, x, y, width, height);
}
pdAppearanceStream.setBBox(new PDRectangle(x, y, width, height));
PDAppearanceDictionary pdAppearanceDictionary =
annotationWidget.getAppearance();
if (pdAppearanceDictionary == null) {
pdAppearanceDictionary = new PDAppearanceDictionary();
annotationWidget.setAppearance(pdAppearanceDictionary);
}
pdAppearanceDictionary.setNormalAppearance(pdAppearanceStream);
}
}
}
{code}
> PdfBox images Alignation Issue after using flatten function
> -----------------------------------------------------------
>
> Key: PDFBOX-5343
> URL: https://issues.apache.org/jira/browse/PDFBOX-5343
> Project: PDFBox
> Issue Type: Bug
> Components: AcroForm
> Affects Versions: 3.0.0 PDFBox
> Reporter: Mohamed Sameem
> Priority: Major
> Labels: PDFBox, acroform, bug, flatten, pdf
> Attachments: Screenshot_2021-12-15-22-49-00-277_com.adobe.reader.jpg,
> Screenshot_2021-12-15-22-50-18-641_com.adobe.reader.jpg, bug.pdf,
> irregular.png, irregular2.png
>
>
> Using flatten function of pdfbox changes the position of images from the
> required box to be filled.I would like to know if there is a fix for this. I
> would like the pdfbox team to look into it.Images attached The images are
> moving out of their respective position after using PDAcroForm.flatten()
> Images attached
> https://stackoverflow.com/questions/70361091/pdfbox-images-alignation-issue-after-using-flatten-function
--
This message was sent by Atlassian Jira
(v8.20.1#820001)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]