相关疑难解决方法(0)

PDFBox嵌入式TTF字体不起作用

我正在使用PDFBox从现有的PDF模板构建文档,因此它会打开文件,向其中添加文本并保存.除了尝试使用外部TTF字体外,它运行良好.我尝试了不同的东西,并搜索了2天的解决方案,但在PDFBox上没有太多.

这是一些代码,使用字体"Tardy Kid",因为它不能被误认为是其他任何东西,并且不太可能是任何标准库的一部分.

代码执行正常,从println显示"TardyKid"(显示字体已加载且名称可获取),并显示文本 - 但它在Helvetica中.getStringWidth()用于计算宽度的代码的更复杂部分似乎也表示宽度表的成功加载.它只是没有正确显示.

代码在较大程序的上下文中运行,该程序打开现有PDF文档(模板)并向其添加文本.这一切似乎都很好,除了

 public void setText ( PDDocument document, String text ) throws IOException {
     int lastPage = document.getNumberOfPages() - 1;
     PDPage page = (PDPage) document.getDocumentCatalog().getAllPages().get(lastPage);
     PDPageContentStream contentStream = null;
     try {
         contentStream = new PDPageContentStream(document,page,true,true,false);
         File fontFile = new File(m_fontDir, "Tardy_Kid.ttf");
         PDFont font = PDTrueTypeFont.loadTTF(document, fontFile);
         Color color =  new Color(196, 18, 47);
         float x = 100f, y = 700f;
         System.out.println(font.getBaseFont());
         contentStream.setFont(font, 32);
         contentStream.setNonStrokingColor(color);
         contentStream.beginText();
         contentStream.moveTextPositionByAmount(x,y);
         contentStream.drawString(text);
         contentStream.endText();
     } finally { …
Run Code Online (Sandbox Code Playgroud)

java truetype pdfbox

7
推荐指数
1
解决办法
8173
查看次数

PDFBox 2.0 创建签名字段并保存已签名文档的增量

我已经在这里阅读了有关 saveIncremental 是如何工作的,我的最终结果需要类似于这里,以我设法根据签名字段本身创建具有多个可视化的可见​​签名的方式(不像响应中那样,而是回复对我帮助很大)。为了详细说明标题,我现在的基本任务是在已签名的文档上创建一个空的签名字段,而不破坏现有的签名。但是,此处的示例不适用于 saveIncremental。我已将以下代码片段(改编)添加到主函数的末尾,但没有结果:

    acroForm.setSignaturesExist(true);
    acroForm.setAppendOnly(true);
    acroForm.getCOSObject().setDirect(true);
// ...


COSDictionary pageTreeObject = pdPage.getCOSObject(); 
while (pageTreeObject != null) {
    pageTreeObject.setNeedToBeUpdated(true);
    pageTreeObject = (COSDictionary) pageTreeObject.getDictionaryObject(COSName.PARENT);
}
Run Code Online (Sandbox Code Playgroud)

生成的文件不包含任何签名字段。我尝试将 COSObject.needToBeUpdated(true) 更新为 acroform、signatureField、pddocument、page、widget,但没有结果。签名字段仅在我正常保存时出现。

编辑:我已设法添加一个空签名字段(在 COSObject.needToBeUpdated 链上编辑的代码),但它破坏了现有签名。

在此输入图像描述

我想念什么?谢谢!

我的实际代码:

public class CreateEmptySignatureForm {

public static void main(String[] args) throws IOException
{
    InputStream resource = new FileInputStream("test-semnat.pdf");
    PDDocument document = PDDocument.load(resource);
            
    PDPage page = document.getPage(0);

    // Add a new AcroForm and add that to the document
    PDAcroForm acroForm = new PDAcroForm(document); …
Run Code Online (Sandbox Code Playgroud)

java pdfbox

6
推荐指数
1
解决办法
2709
查看次数

PDFBox - 打开并保存签名的 pdf 会使我的签名无效

我正在尝试学习使用 Apache 的 pdfBox 来处理工作中的数字签名文档。在测试过程中,我创建了一个完全空的 pdf 文档。

然后我通过 Adob​​e reader 使用带有证书的签名功能对文档进行了签名。

我尝试使用 pdfBox 打开、保存和关闭签名文件,而不进行任何修改。但是,一旦我在 Adob​​e 中打开该文件,这些文件就不再有效。

Adobe 告诉我:“此签名中包含的格式或信息存在错误(支持信息:SigDict/Contents 非法数据)”

由于我没有修改文件的内容,直观上不应该有任何问题,签名应该仍然有效,但事实并非如此,我不知道解决方案是什么(谷歌搜索没有结果)。

我如何创建文档:

@Test
public void createEmptyPDF() throws IOException {
    String path = "path to file";
    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage(page);
    document.save(path);
    document.close();
}
Run Code Online (Sandbox Code Playgroud)

然后我用 adobe 签名并通过以下方式传递:

 @Test
public void copySignedDocument() throws IOException {
    String path = "path to file";
    File file = new File(path);
    PDDocument document = PDDocument.load(file);
    document.save(file);
    document.close();

    //just opening and saving the …
Run Code Online (Sandbox Code Playgroud)

java pdf adobe digital-signature pdfbox

5
推荐指数
1
解决办法
6654
查看次数

标签 统计

java ×3

pdfbox ×3

adobe ×1

digital-signature ×1

pdf ×1

truetype ×1