使用Java定义模板和操作

问题描述 投票:0回答:1

作为我的项目要求的一部分,我正在寻找创建预定义的模板,这些模板具有预定义的固定变量部分的字体和缩进,我想在运行时替换它们并生成PDF。这些可能是替换单个变量示例#Name或插入重复组#List的订单。

我正在寻找使用标签创建PDF,然后通过PDFBox阅读并替换它们。但是,似乎在不知道预定义位置的情况下,无法使用PDFBox替换文本。

有什么替代方案我需要看一下吗?我需要能够在每个页面上有单独的标题,并能够用变量替换标签。

谢谢兔子

java pdf reporting
1个回答
0
投票

为您的字体和布局创建模板是个好主意。您可以使用文字处理器文档(doc / docx / odt),填充它们然后转换为PDF,而不是使用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工作。

我希望有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.