java 替换PDF中给定的字符串,保持原始样式[关闭]

gblwokeq  于 5个月前  发布在  Java
关注(0)|答案(1)|浏览(60)

已关闭。此问题需要更多focused。目前不接受回答。
**要改进此问题吗?**更新此问题,使其仅针对editing this post的一个问题。

6天前关闭
Improve this question
考虑以下情况;我有一个包含以下行的PDF-

line 1: I love to **write** code
line 2: I love to write *java* code
line 3: I love to write java code that replaces some texts(underline) in pdf.

字符串
在第1行,write是粗体,具有Arial字体。
在第2行,java是斜体,具有Nunito字体。
在第3行,texts被加下划线,具有Times New Roman字体。
我想做的是
writecheck
javaperl
textswords
保持完全相同的字体和样式每个人都有。我一直试图实现这一点使用Itext7java库,并通过了大量的资源对SO,博客和书籍,但没有满足我的确切要求。
到目前为止,我可以用相同的字体替换pdf中的给定单词(如果pdf只包含一种字体)。虽然提取的字体大小与原始字体不同,但我必须手动放置。

public static void main(String[] args) throws IOException {

     PdfReader reader = new PdfReader(SOURCE);
     PdfWriter writer = new PdfWriter(DESTINATION);
     PdfDocument pdfDocument = new PdfDocument(reader, writer);

     TextPropertiesExtractionStrategy extractionStrategy = new TextPropertiesExtractionStrategy();
            new PdfCanvasProcessor(extractionStrategy).processPageContent(pdfDocument.getPage(1));
     System.out.println("Font Name: " + extractionStrategy.getFontName());
     System.out.println("Font Size: " + extractionStrategy.getFontSize());
     System.out.println("Text Color: " + extractionStrategy.getTextColor().getColorSpace().toString());

}

private static class TextPropertiesExtractionStrategy implements ITextExtractionStrategy {
        private String fontName;
        private float fontSize;
        private Color textColor;
        private PdfFont font;

        @Override
        public void eventOccurred(IEventData data, EventType type) {
            if (data instanceof TextRenderInfo) {
                TextRenderInfo textRenderInfo = (TextRenderInfo) data;

                // Get font information
                font = textRenderInfo.getFont();
                fontName = font.getFontProgram().getFontNames().getFontName();
                fontSize = textRenderInfo.getFontSize();

                // Get text color information
                textColor = textRenderInfo.getFillColor();
            }
        }

        @Override
        public Set<EventType> getSupportedEvents() {
            return null;
        }

        @Override
        public String getResultantText() {
            return null;
        }

        public String getFontName() {
            return fontName;
        }

        public float getFontSize() {
            return fontSize;
        }

        public PdfFont getFont() {
            return font;
        }

        public Color getTextColor() {
            return textColor;
        }
    }


我对任何其他开源库或语言都持开放态度,比如python [我也尝试过MuPdf],只要它能解决这个特定的问题。

mzsu5hc0

mzsu5hc01#

忽略语言PDF的构造方式通常不适合于组件的本地编辑。编辑器基本上需要替换现有的条目并编写新的内容。以w r it e需要更改为check的第一个更改为例。这“应该很容易”,它不是嵌入的字体,并且是相同的二进制数字(字节)数,可能会出现什么问题?
100d1x

的字符串
因此,字体位置完全被比例字体的字符宽度改变所扰乱,因此编辑需要将整个文本块视为全新的。
好吧,让我们改变块样式,马上我们看到为什么你不能简单地替换字体样式的字母,因为它们不会被放置在正确的间距。

OK java 更直接,但它是一种嵌入式字体,所以如果没有包含一个字母,您将看到一个框或空白。

由于它是一个PDF texts是不一样的words,所以请注意下面的行是一个完全不同的对象(没有下划线字体),需要一个完全单独的编辑。

因此,更改PDF文本的最有效方法是使用一个抬头GUI文字处理器,其中这些中断和其他中断都可以通过人工判断来补偿。

相关问题