gpt4 book ai didi

java - 使用 iText7 设置突出显示注释的外观流

转载 作者:行者123 更新时间:2023-11-30 05:52:25 26 4
gpt4 key购买 nike

我有一个 PDF 查看器,如果没有外观流,则不会显示突出显示。我正在尝试使用 java 中的 iText 7 core 来尝试向 PDF 添加突出显示注释,但这些注释没有外观流,因此我希望在将注释写入 PDF 文件时尝试自己添加它们。

我遇到过this旧答案,但它适用于 C# 和 iText 5,我似乎不知道如何在 iText 7 中复制它并获得成功的结果。

所以我的问题是:如何在 iText 7 核心中正在运行的突出显示注释上设置外观流?

我所得到的最远的代码如下所示。我使用 RegexBasedLocationExtrationStrategy 类来查找 pdf 中所有搜索词的位置。

RegexBasedLocationExtractionStrategy evntlstnr = new RegexBasedLocationExtractionStrategy(pattern);
for (int pIdx = 0; pIdx < pdfDoc.getNumberOfPages(); ++pIdx) {
final PdfPage page = pdfDoc.getPage(pIdx + 1);
new PdfCanvasProcessor(evntlstnr).processPageContent(page);
Collection<IPdfTextLocation> locations = evntlstnr.getResultantLocations();
for (IPdfTextLocation location : locations) {
Rectangle rect = location.getRectangle();
// Specify quad points in Z-like order
// [0,1] x1,y1 [2,3] x2,y2
// [4,5] x3,y3 [6,7] x4,y4
float[] quads = new float[8];
quads[0] = rect.getX();
quads[1] = rect.getY() + rect.getHeight();
quads[2] = rect.getX() + rect.getWidth();
quads[3] = quads[1];
quads[4] = quads[0];
quads[5] = rect.getY();
quads[6] = quads[2];
quads[7] = quads[5];

Color highlightColor = new DeviceRgb(0f, 0f, 1f);
PdfTextMarkupAnnotation highlight = PdfTextMarkupAnnotation.createHighLight(rect, quads);
highlight.setColor(highlightColor);


Rectangle appearRect = new Rectangle(0f, 0f, rect.getWidth(), rect.getHeight());
PdfFormXObject appearObj = new PdfFormXObject(appearRect);
final PdfResources appearRes = appearObj.getResources();

PdfExtGState extGState = new PdfExtGState();
extGState.setBlendMode(PdfExtGState.BM_MULTIPLY);
appearRes.addExtGState(extGState);
appearObj.setBBox(new PdfArray(new float[] {0f, 0f, rect.getWidth(), rect.getHeight()}));

PdfShading appearShading = new PdfShading.Axial(highlightColor.getColorSpace(), 0f, 0f, highlightColor.getColorValue(), 1f, 1f, highlightColor.getColorValue());
appearRes.addShading(appearShading);

appearRes.addColorSpace(highlightColor.getColorSpace());

PdfAnnotationAppearance appearance = new PdfAnnotationAppearance(appearObj.getPdfObject());
highlight.setNormalAppearance(appearance);
highlight.setFlag(PdfAnnotation.PRINT);
page.addAnnotation(highlight);
}
}

最佳答案

使用塞缪尔的答案,我偶然找到了一个可行的答案。

我对 PDF 标准和这个框架(iText)并不抱有任何期望,但根据下面我的工作示例,我的假设是,我试图为突出显示编写的矩形是一种粗略的后备方法当查看器无法呈现注释时“伪造”突出显示矩形(因为它们没有外观流)。意识到这两个操作没有关联,我来到了下面所示的工作示例。希望这对将来的其他人有帮助。

RegexBasedLocationExtractionStrategy evntlstnr = new RegexBasedLocationExtractionStrategy(pattern);
for (int pIdx = 0; pIdx < pdfDoc.getNumberOfPages(); ++pIdx) {
final PdfPage page = pdfDoc.getPage(pIdx + 1);
new PdfCanvasProcessor(evntlstnr).processPageContent(page);
Collection<IPdfTextLocation> locations = evntlstnr.getResultantLocations();
for (IPdfTextLocation location : locations) {
Rectangle rect = location.getRectangle();
// Specify quad points in Z-like order
// [0,1] x1,y1 [2,3] x2,y2
// [4,5] x3,y3 [6,7] x4,y4
float[] quads = new float[8];
quads[0] = rect.getX();
quads[1] = rect.getY() + rect.getHeight();
quads[2] = rect.getX() + rect.getWidth();
quads[3] = quads[1];
quads[4] = quads[0];
quads[5] = rect.getY();
quads[6] = quads[2];
quads[7] = quads[5];

Color highlightColor = new DeviceRgb(1f, 1f, 0f);

PdfTextMarkupAnnotation highlight = PdfTextMarkupAnnotation.createHighLight(rect, quads);
highlight.setColor(highlightColor);
highlight.setFlag(PdfAnnotation.PRINT);
page.addAnnotation(highlight);

PdfCanvas canvas = new PdfCanvas(page);
PdfExtGState extGState = new PdfExtGState();
extGState.setBlendMode(PdfExtGState.BM_MULTIPLY);
canvas.setExtGState(extGState);
canvas.rectangle(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
canvas.setFillColor(highlightColor);
canvas.fill();
canvas.release();
}
}

关于java - 使用 iText7 设置突出显示注释的外观流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53671074/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com