문제 설명
동적 컨트롤로 PDF 양식 템플릿 수정 (Modify PDF Form Template with dynamic controls)
여기서 빈 컨트롤이 있는 기존 PDF 양식(이전에 Open Office로 생성)을 열고 iTextSharp()를 사용하여 값을 설정하는 코드를 생성하려고 합니다. 나는 여전히 내가 해야 할 일을 하는지 알아보기 위해 iTextSharp를 테스트 중이며 지금까지 대답은 아니오입니다.
인터넷에서 찾은 내용에 따라 아래에서 내가 한 작업을 참조하세요. :
string fileNameExisting = @"PdfTemplate.pdf";
string fileNameNew = @"new.pdf";
using (var existingFileStream = new FileStream(fileNameExisting, FileMode.Open))
using (var newFileStream = new FileStream(fileNameNew, FileMode.Create))
{
// Open existing PDF
var pdfReader = new PdfReader(existingFileStream);
// PdfStamper, which will create
var stamper = new PdfStamper(pdfReader, newFileStream);
var form = stamper.AcroFields;
var fieldKeys = form.Fields.Keys;
foreach (string fieldKey in fieldKeys)
{
bool result = form.SetField(fieldKey, "A lot of text here.");
}
stamper.Close();
pdfReader.Close();
}
문제 1
iTextSharp는 Open Office(예: 텍스트 상자)의 '컨트롤' 요소만 인식합니다. PDF 템플릿에 표를 추가하려고 했지만 필드에 나타나지 않습니다. 즉, 사용하는 것이 정말 제한적입니다.
이슈 2
필드에 텍스트를 설정하면 텍스트 줄 바꿈이 없습니다. , 컨트롤의 크기는 동적이지 않으므로 텍스트가 너무 길면 모두 표시되지 않습니다. PDF는 인쇄하기 때문에 스크롤바를 사용할 수 없습니다.
해보았습니다
첫 번째 문제의 경우 Word로 PDF 양식을 만들었습니다. Open Office Writer 대신. 그러나 iTextSharp는 Word의 컨트롤을 인식하지 못합니다. 내 필드 컬렉션은 비어 있습니다.
두 번째 문제의 경우 Open Office에서 컨트롤의 모든 속성을 수정하려고 시도했으며 인터넷에서 다음을 확인했습니다. 누군가가 해결책을 가지고 있는지 확인하십시오. 하지만 내가 알기로는 AcroFields처럼 크기가 고정되어 있어서 컨트롤을 동적으로 만들 수 없고 나중에 iTextSharp로 크기를 변경할 수도 없습니다.
나는 누군가가 같은 상황을 겪었고 iTextSharp 또는 무료이든 아니든 다른 라이브러리로 나를 안내할 수 있기를 바랐습니다. 나는 내 자신의 사업을 운영하고 있기 때문에 £2000 라이선스를 감당할 수 없지만 제공해야 할 제안에 대해 열려 있습니다. 마지막 옵션은 iTextSharp를 사용하여 PDF를 처음부터 만드는 것이지만 수정만큼 빠르고 쉽게 생성할 수 없으며 PDF를 업데이트할 때마다 회사에서 코드를 변경해야 한다는 것을 의미합니다. 그 솔루션이 마음에 들지 않습니다.
참조 솔루션
방법 1:
Issue 1:
A table is not a form field. Please read the PDF specification, more specifically ISO‑32000‑1:
There is no such thing as a dynamic table in PDF. That is only possible in XFA (which is XML wrapped in a PDF file), but XFA is being deprecated. At iText, we'll release a (closed source) product in February 2017 for dynamic documents.
Issue 2:
The text only wraps if the field is defined as a multi‑line text field. See for instance https://developers.itextpdf.com/question/how‑get‑row‑count‑multiline‑field
The font size only adapts to the size of the field if you set the font size to 0: Set AcroField Text Size to Auto
Summarized:
Dynamic forms and PDF either require XFA in which case you need to buy Adobe LiveCycle ES (which is way above your budget), or you need to wait until iText Group releases its dynamics forms project (but that will also be more expensive than £2000).
(by nanonymous、Bruno Lowagie)