좋은 생각이다. PDF 문서를 템플릿으로 사용하는 대신 워드 프로세서 문서 (doc/docx/odt)를 사용하여 채우고 PDF로 변환 할 수 있습니다.
이 방법이 귀하의 목적에 맞는 경우 Libre Office Java API를 사용하여 템플릿을 조작하고 PDF로 저장할 수 있습니다. 즉, DOC 또는 ODT 파일을 템플릿 (다양한 헤더 포함) 및 데이터 용 자리 표시 자로 만들면 Java 프로그램이 Libre Office를 제어하여 문서를로드하고 자리 표시자를 업데이트하고 PDF로 저장할 수 있습니다. 다음 Java 코드는 템플릿에 3 개 자리를 대체하고 PDF로 저장합니다
// Initialise
XComponentContext xContext = Bootstrap.bootstrap();
XMultiComponentFactory xMCF = xContext.getServiceManager();
Object oDesktop = xMCF.createInstanceWithContext(
"com.sun.star.frame.Desktop", xContext);
XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(
XDesktop.class, oDesktop);
// Load the Document
String workingDir = "C:/projects/";
String myTemplate = workingDir + "letterTemplate.doc";
if (!new File(myTemplate).canRead()) {
throw new RuntimeException("Cannot load template:" + new File(myTemplate));
}
XComponentLoader xCompLoader = (XComponentLoader) UnoRuntime
.queryInterface(com.sun.star.frame.XComponentLoader.class, xDesktop);
String sUrl = "file:///" + myTemplate;
PropertyValue[] propertyValues = new PropertyValue[0];
propertyValues = new PropertyValue[1];
propertyValues[0] = new PropertyValue();
propertyValues[0].Name = "Hidden";
propertyValues[0].Value = new Boolean(true);
XComponent xComp = xCompLoader.loadComponentFromURL(
sUrl, "_blank", 0, propertyValues);
// Manipulate
XReplaceDescriptor xReplaceDescr = null;
XReplaceable xReplaceable = null;
XTextDocument xTextDocument = (XTextDocument) UnoRuntime
.queryInterface(XTextDocument.class, xComp);
xReplaceable = (XReplaceable) UnoRuntime
.queryInterface(XReplaceable.class,
xTextDocument);
xReplaceDescr = (XReplaceDescriptor) xReplaceable
.createReplaceDescriptor();
// mail merge the date
xReplaceDescr.setSearchString("<date>");
xReplaceDescr.setReplaceString(new Date().toString());
xReplaceable.replaceAll(xReplaceDescr);
// mail merge the addressee
xReplaceDescr.setSearchString("<addressee>");
xReplaceDescr.setReplaceString("Best Friend");
xReplaceable.replaceAll(xReplaceDescr);
// mail merge the signatory
xReplaceDescr.setSearchString("<signatory>");
xReplaceDescr.setReplaceString("John Steady");
xReplaceable.replaceAll(xReplaceDescr);
// save as a PDF
XStorable xStorable = (XStorable) UnoRuntime
.queryInterface(XStorable.class, xComp);
propertyValues = new PropertyValue[2];
// Setting the flag for overwriting
propertyValues[0] = new PropertyValue();
propertyValues[0].Name = "Overwrite";
propertyValues[0].Value = new Boolean(true);
// Setting the filter name
propertyValues[1] = new PropertyValue();
propertyValues[1].Name = "FilterName";
propertyValues[1].Value = "writer_pdf_Export";
// Appending the favoured extension to the origin document name
String myResult = workingDir + "letter1.pdf";
xStorable.storeToURL("file:///" + myResult, propertyValues);
System.out.println("Saved " + myResult);
// shutdown
xDesktop.terminate();
당신은이 블로그 java-convert-word-to-pdf-part-1의 코드에 대한 설명을 찾을 수 있습니다. 나는 Docmosis를 위해 일한다는 것을 유의하십시오.
도움이 되었기를 바랍니다.
PDF는 워드 프로세싱 형식이 아니므로 "한 단어를 다른 단어로 바꾸려는 기존 PDF 문서가 있습니다."라고 PDF 전문가가 듣습니다. "양파 스프가 있고, 토마토와 양파. " 나는 농담을하지 않는다고 가정하므로 질문에 웃지 않으려 고 노력 하겠지만 PDF 양식을 보았습니까? PDF AcroForm 기술을 사용하고 있습니까? –
해당 PDF 출처가 있습니까? 어쩌면 그것들은 읽을 수있는 * 형식을 가지고있을 것입니다. PDF로 변환 할 때 단어 정보와 콘텐츠 구조에 대한 많은 정보가 누락되었음을 알아야합니다. PDF에는 *이 좌표에서 일부 획과 그 좌표에서 다른 획을 배치하는 것과 같은 내용이 포함되어 있습니다. 그래서 PDF를 조작하는 것이 어렵고 훌륭한 편집자에게 많은 비용이 드는 이유이기도합니다. – Zabuza