PDF 텍스트 상자 주석을 채우는 방법
다음 코드로 텍스트 상자 주석을 채울 수 있지만 텍스트가 Chrome 및 기타 Webkit 기반 브라우저에는 표시되지만 Adobe Acrobat과 같은 특정 리더에는 표시되지 않습니다. 필자가 채우려는 PDF는 AcroForms 또는 FDF를 사용하지 않습니다. Apache PDFBox를 사용하고 있지만 언어 / 플랫폼 간에도 PDF 라이브러리에 많은 차이가 있다고 생각하지 않습니다.
// edited for brevity
PDAnnotation annotation = doc.getPages().get(0).getAnnotations().get(0);
COSDictionary cosObject = annotation.getCOSObject();
cosObject.setString(COSName.V, content);
예시 문서는 IRS 양식 W-4 입니다.
내가 지금까지 시도한 것
내 PDF 출력을 Chrome으로 채워진 문서와 비교해 보았지만 유일한 차이점은 기본 모양 (DA) 속성에 있습니다. 다음과 같이 기본 모양 텍스트 콘텐츠를 설정하려고 시도했지만 아무 소용이 없습니다.
COSString defaultAppearance = (COSString)cosObject.getItem(COSName.DA);
COSString newAppearance = new COSString(defaultAppearance.getString() + "0 0 Td (" + value + ") Tj");
cosObject.setItem(COSName.DA, newAppearance);
나는 또한 유망하게 들리는 몇 가지 플래그를 엉망으로 만들었습니다.
int FLAG_PRINT = 4;
int FLAG_READ_ONLY = 64;
annotation.setAnnotationFlags(annotation.getAnnotationFlags() | FLAG_PRINT | FLAG_READ_ONLY);
나는 또한 다른 속성을 시도했습니다.
cosObject.setString(COSName.CONTENTS, content);
PDF 1.7 사양 의 관련 섹션 이 12.7.4.3 이라고 생각합니다 .
내가 무엇을 놓치고 있습니까?
답변
PDF는 곡예사 필드를 사용합니다. 위젯 주석은 필드의 시각적 표현입니다. 원하는 것은 필드를 설정하는 것입니다. 다음은 소스 코드 다운로드의 SetField.java 예제입니다. 파일 이름, 필드 이름 (이름은 "topmostSubform [0] .Page1 [0] .Step1a [0] .f1_01 [0]") 및 값을 사용하여 호출합니다.
필드 이름을 얻으려면 PDFDebugger를 다운로드하고 설정하려는 필드 위로 마우스를 가져갑니다.
다음은 필드가 설정된 후의 모습입니다.

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.pdfbox.examples.interactive.form;
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDCheckBox;
import org.apache.pdfbox.pdmodel.interactive.form.PDComboBox;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;
import org.apache.pdfbox.pdmodel.interactive.form.PDListBox;
import org.apache.pdfbox.pdmodel.interactive.form.PDRadioButton;
import org.apache.pdfbox.pdmodel.interactive.form.PDTextField;
/**
* This example will take a PDF document and set a form field in it.
*
* @author Ben Litchfield
*
*/
public class SetField
{
/**
* This will set a single field in the document.
*
* @param pdfDocument The PDF to set the field in.
* @param name The name of the field to set.
* @param value The new value of the field.
*
* @throws IOException If there is an error setting the field.
*/
public void setField(PDDocument pdfDocument, String name, String value) throws IOException
{
PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
PDField field = acroForm.getField(name);
if (field != null)
{
if (field instanceof PDCheckBox)
{
if (value.isEmpty())
((PDCheckBox) field).unCheck();
else
((PDCheckBox) field).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)
{
field.setValue(value);
}
}
else
{
System.err.println("No field found with name:" + name);
}
}
/**
* This will read a PDF file and set a field and then write it the pdf out
* again. <br>
* see usage() for commandline
*
* @param args command line arguments
*
* @throws IOException If there is an error importing the FDF document.
*/
public static void main(String[] args) throws IOException
{
SetField setter = new SetField();
setter.setField(args);
}
private void setField(String[] args) throws IOException
{
PDDocument pdf = null;
try
{
if (args.length != 3)
{
usage();
}
else
{
SetField example = new SetField();
pdf = PDDocument.load(new File(args[0]));
example.setField(pdf, args[1], args[2]);
pdf.save(args[0]);
}
}
finally
{
if (pdf != null)
{
pdf.close();
}
}
}
/**
* This will print out a message telling how to use this example.
*/
private static void usage()
{
System.err.println("usage: org.apache.pdfbox.examples.interactive.form.SetField <pdf-file> <field-name> <field-value>");
}
}