【问题标题】:Pdfbox not drawing text in specific file onlyPdfbox仅在特定文件中不绘制文本
【发布时间】:2015-02-21 10:00:11
【问题描述】:

我使用 PDFBox 1.8.8 并尝试向现有 pdf 文件添加新文本。问题是我添加的文本正在为这个特定的 pdf 压缩

original pdf(我认为这个文件是问题的根源)

(它适用于其他 pdf)。

image of problem with explanation

最初并没有显示,但我修复了这个推荐的问题(设置字体显示模式)

content.appendRawCommands("0 Tr ");  

我正在使用 pdf 矩阵并检查了几乎所有内容......但看不出我该如何解决这个问题......

content.setTextMatrix(1, 0, 0, 1, 0, 0);

问题:

1) 有什么方法可以强制 pdfbox 使用正常的字体设置并忽略页面上当前的字体选项

2) 有什么方法可以读取当前的字体设置,并且可以使用 content.setTextMatrix(带有高度和宽度)

3) 我的 pdf 中使用什么模式来存档这种转换...

我的代码

private static void writePdfBoxStamp(PDDocument document, int page,
        float topLeftX, float topLeftY, float width, float height,
        String text, float  itemFontSize, int[] color, int rotation) throws IOException {

    PDPage pdfPage = (PDPage) document.getDocumentCatalog().getAllPages().get(page);
    PDPageContentStream content = new PDPageContentStream(document, pdfPage, true, true);


    // COSDictionary pageFonts = pdfPage.getResources().getCOSDictionary();


    //border draw
    float lineWidth = 2;

    float bottomLeftX = topLeftX;
    float bottomLeftY = topLeftY - height;
    float topRightX = topLeftX + width;
    float topRightY = topLeftY;

    float radius = 10;

    content.setLineWidth(lineWidth);
    content.setStrokingColor(new Color(color[0], color[1], color[2]));


    content.setNonStrokingColor(new Color(color[0], color[1], color[2]));

    PDFont font = PDType1Font.HELVETICA_BOLD;
    int fontSize = (int) itemFontSize;


    float linesHeight = font.getFontDescriptor().getFontBoundingBox()
            .getHeight()
            / 1000 * fontSize;
    float txtLineWidth = font.getStringWidth(text)
            / 1000 * fontSize;

    content.setFont(font, fontSize);

    content.beginText();

    content.setFont(font, fontSize);

    float  posTextX = topLeftX + (width-txtLineWidth)/2;
    float  posTextY = topLeftY  - height/2 - linesHeight/2 + lineWidth;



    if (rotation > 0) {
        int  step = 0;
        // clockwise rotation
        if (rotation == 90) {
            step = 6;
            posTextX = topLeftX + (width + linesHeight)/2 - lineWidth;
            posTextY = topLeftY - (height + txtLineWidth)/2;
        }
        if (rotation == 270) {
            step = -6;
            posTextX = topLeftX + (width - linesHeight/2)/2;
            posTextY = topLeftY - (height - txtLineWidth)/2;
        }
        content.setTextRotation(-step*Math.PI*0.25, posTextX, posTextY);

    } else {
        content.moveTextPositionByAmount(posTextX,posTextY);

    }

    content.appendRawCommands("0 Tr "); //set normal text procession

    content.drawString(text);


    content.endText();

    content.stroke();


    content.close();
}

【问题讨论】:

  • pdfbox generated file也有链接
  • 您可以编辑您的问题以在其中包含链接,有些人不阅读 cmets。

标签: pdf pdfbox


【解决方案1】:

我用 PDFDebugger 查看了您的结果 PDF,它在页面的流数组中有两个流,第二个流是“已批准”的东西。所以你可以做的是将原始流包含在 q....Q 中以保存和恢复设置。我尝试使用 NOTEPAD++ 在 PDF 中进行编辑(我首先使用 WriteDecodedDoc 命令行实用程序将其解压缩)。

还有其他策略可能比我的反应更好,例如LayerUtility,或使用透明图像代替(RubberStampWithImage 示例)。

更新: 我看到你在 JIRA 上问过同样的问题,Andreas 的回答也很好: https://issues.apache.org/jira/browse/PDFBOX-2581

【讨论】:

  • Andreas 的回答尤其是最容易在应用程序中使用。
  • 安德烈亚斯的回答有效!谢谢大家的帮助!
猜你喜欢
  • 2013-11-24
  • 1970-01-01
  • 2019-05-27
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 2015-07-13
  • 1970-01-01
相关资源
最近更新 更多