iText7을 사용하여 PDFButtonFormField에 이미지를 추가할 때 종횡비를 유지하는 방법 (How to Maintain Aspect Ratio when Adding an Image to a PDFButtonFormField using iText7)


문제 설명

iText7을 사용하여 PDFButtonFormField에 이미지를 추가할 때 종횡비를 유지하는 방법 (How to Maintain Aspect Ratio when Adding an Image to a PDFButtonFormField using iText7)

PDF 양식의 양식 필드가 있는 사용자로부터 이미지를 수집하고 있습니다. 필드가 비어 있으면 사용자가 Acrobat에서 필드를 채울 수 있고 iText7을 사용하여 양식에서 성공적으로 읽을 수 있습니다. 사용자가 이전에 이미지를 업로드한 경우 양식 필드에 이미 로드된 해당 이미지를 제공하고 다른 이미지를 선택하여 제출할 수 있도록 하고 싶습니다. iText를 사용하면 양식을 이미지로 채울 수 있지만 양식 필드의 직사각형 크기로 크기를 조정하여 이미지의 종횡비를 왜곡합니다.

iText의 setImage()<를 얻을 수 있는 방법이 있습니까? /code> 메서드를 사용하여 이미지를 로드할 때 가로 세로 비율을 유지합니까?

이미지를 로드하기 전에 다음 코드를 사용하여 양식 필드의 사각형을 이미지 가로 세로 비율에 맞게 수정하려고 했습니다.<


참조 솔루션

방법 1:

The default behavior of drawing button field appearance that doesn't preserve image's aspect ratio is quite complicated to override. Instead I can suggest to generate appearance manually, making it exactly as you want it.

// get widget dictionary
List<PdfWidgetAnnotation> widgets = pushButtonField.getWidgets();
PdfDictionary widgetDict;
if (widgets.size() > 0) {
    widgetDict = widgets.get(0).getPdfObject();
} else {
    // widgets.size() == 0 shouldn't really happen to properly created
    // existing fields, but let's do it just in case
    widgetDict = pushButtonField.getPdfObject();
}
Rectangle origRect = widgetDict.getAsRectangle(PdfName.Rect);
float borderWidth = pushButtonField.getBorderWidth();

String imgPath = ... // path to image file

// draw custom appearance preserving original field sizes
PdfFormXObject pdfFormXObject = new PdfFormXObject(new Rectangle(origRect.getWidth() ‑ borderWidth * 2, origRect.getHeight() ‑ borderWidth * 2));
Canvas canvas = new Canvas(pdfFormXObject, pdfDoc);
// Image class preserves aspect ratio by default
Image image = new Image(ImageDataFactory.create(imgPath))
        .setAutoScale(true)
        .setHorizontalAlignment(HorizontalAlignment.CENTER);
Div container = new Div()
        .setMargin(borderWidth).add(image)
        .setVerticalAlignment(VerticalAlignment.MIDDLE)
        .setFillAvailableArea(true);
canvas.add(container);
canvas.close();

// override original appearance with new one
PdfDictionary apDict = new PdfDictionary();
widgetDict.put(PdfName.AP, apDict);
apDict.put(PdfName.N, pdfFormXObject.getPdfObject());

// mark widgetDict as modified in order to save its changes in append mode
widgetDict.setModified();

The last line (calling setModified()) is required to be done for all low level PdfObjects modifications if you are using properties.useAppendMode();. Otherwise the change will be not saved.

(by Stephen SchultzYulian Gaponenko)

참조 문서

  1. How to Maintain Aspect Ratio when Adding an Image to a PDFButtonFormField using iText7 (CC BY‑SA 2.5/3.0/4.0)

#itext #image #pdf #java






관련 질문

iText로 가변 페이지 PDF를 생성하려고 할 때 예외 (Exception when attempting to generate variable-page PDF with iText)

Icepdf 특수 문자 렌더링 문제 (Icepdf special character rendering issue)

JAVA에서 PDF 양식 테이블 채우기 (Filling PDF form tables in JAVA)

itext를 사용하여 poi에서 차트 내보내기 (Exporting charts from poi using itext)

표 셀에 사각형을 그리는 방법은 무엇입니까? (How to draw rectangles in a Table cell?)

동적 컨트롤로 PDF 양식 템플릿 수정 (Modify PDF Form Template with dynamic controls)

itext와 같은 라이브러리로 Java 응용 프로그램을 배포하는 방법은 무엇입니까? (how to distribute a java-application with libraries such as itext?)

종이 Itext7에서 첫 번째 줄과 마지막 줄 속성을 제거합니다. (Remove the first and last lines properties in the paper Itext7)

Java에서 iText 5를 사용하여 여러 셀을 만들고 각 셀에 각 문자를 설정하는 방법 (How to create multiple cells and set each character into each cell using iText 5 in Java)

iTextSharp(또는 iText 5)를 사용하여 다른 PdfImportedPage의 배경으로 전체 PdfImportedPage를 추가하는 방법 (How to add a full PdfImportedPage as background for another PdfImportedPage with iTextSharp (or iText 5))

그래픽 텍스트 효과가 있는 텍스트 요소 (Text element with graphical text effects)

iText7을 사용하여 PDFButtonFormField에 이미지를 추가할 때 종횡비를 유지하는 방법 (How to Maintain Aspect Ratio when Adding an Image to a PDFButtonFormField using iText7)







코멘트