第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

itext7 - 如何在寫入期間過(guò)濾渲染事件

itext7 - 如何在寫入期間過(guò)濾渲染事件

尚方寶劍之說(shuō) 2021-06-18 18:05:32
我想過(guò)濾 RENDER_TEXT 事件,因?yàn)樗鼈儽粚懭胼敵鑫募?。我有一個(gè) PDF,里面有一些我想過(guò)濾掉的文本。我發(fā)現(xiàn)我可以遍歷文檔一次并確定我想要過(guò)濾的渲染事件的特征?,F(xiàn)在我想復(fù)制源文檔的頁(yè)面并跳過(guò)一些 RENDER_TEXT 事件,以便文本不會(huì)出現(xiàn)在目標(biāo)文檔中。我有一個(gè) IEventFilter 可以接受正確的事件。我只需要知道如何將此過(guò)濾器放在文檔編寫器上。目標(biāo)是采用議程格式從 Google 日歷創(chuàng)建的 PDF 并刪除“創(chuàng)建者:”和“日歷:”行。這些行通常由 3 個(gè) RENDER_TEXT 事件組成。我當(dāng)前的代碼如下。我發(fā)現(xiàn)所有具有相同基線 y 坐標(biāo)的 RENDER_TEXT 事件將標(biāo)識(shí)我想要?jiǎng)h除的事件。import java.io.FileNotFoundException;import java.io.IOException;import java.nio.file.Path;import java.nio.file.Paths;import java.util.Collections;import java.util.LinkedList;import java.util.List;import java.util.Set;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import com.itextpdf.kernel.geom.LineSegment;import com.itextpdf.kernel.geom.PageSize;import com.itextpdf.kernel.geom.Rectangle;import com.itextpdf.kernel.pdf.PdfDocument;import com.itextpdf.kernel.pdf.PdfPage;import com.itextpdf.kernel.pdf.PdfReader;import com.itextpdf.kernel.pdf.PdfWriter;import com.itextpdf.kernel.pdf.canvas.parser.EventType;import com.itextpdf.kernel.pdf.canvas.parser.PdfCanvasProcessor;import com.itextpdf.kernel.pdf.canvas.parser.data.IEventData;import com.itextpdf.kernel.pdf.canvas.parser.data.TextRenderInfo;import com.itextpdf.kernel.pdf.canvas.parser.filter.IEventFilter;import com.itextpdf.kernel.pdf.canvas.parser.listener.IEventListener;public class Main {    private static final Logger LOGGER = LogManager.getLogger();    public static void main(String[] args) throws FileNotFoundException, IOException {        final Path src = Paths.get("calendar_2018-08-04_2018-08-19.pdf");        final Path dest = Paths.get("/home/jpschewe/Downloads/calendar_clean.pdf");        final Main app = new Main(src, dest);    }
查看完整描述

1 回答

?
喵喔喔

TA貢獻(xiàn)1735條經(jīng)驗(yàn) 獲得超5個(gè)贊

正如評(píng)論中所建議的,您可以使用PdfCanvasEditorfrom this answer根據(jù)需要從內(nèi)容流中過(guò)濾操作。實(shí)際上,我稍微擴(kuò)展了該類,以便能夠正確支持'和"文本繪制運(yùn)算符。您可以在此處找到該課程。


就像在您的方法中一樣,要清除的行是在第一次運(yùn)行時(shí)確定的:我RegexBasedLocationExtractionStrategy為此使用了一個(gè)實(shí)例。


此后,在該P(yáng)dfCanvasEditor步驟中,將在這些行上繪制文本的指令更改為僅繪制空字符串。


不過(guò),由于不是您檢查的事件導(dǎo)致在此處繪制文本,而是更基本的運(yùn)算符和操作數(shù)結(jié)構(gòu),因此確切的機(jī)制不是從IEventFilter. 但是機(jī)制與您的方法相似。


try (PdfDocument pdfDocument = new PdfDocument(SOURCE_PDF_READER, TARGET_PDF_WRITER)) {

    List<Rectangle> triggerRectangles = new ArrayList<>();


    PdfCanvasEditor editor = new PdfCanvasEditor()

    {

        {

            Field field = PdfCanvasProcessor.class.getDeclaredField("textMatrix");

            field.setAccessible(true);

            textMatrixField = field;

        }


        @Override

        protected void nextOperation(PdfLiteral operator, List<PdfObject> operands) {

            try {

                recentTextMatrix = (Matrix)textMatrixField.get(this);

            } catch (IllegalArgumentException | IllegalAccessException e) {

                throw new RuntimeException(e);

            }

        }


        @Override

        protected void write(PdfCanvasProcessor processor, PdfLiteral operator, List<PdfObject> operands)

        {

            String operatorString = operator.toString();


            if (TEXT_SHOWING_OPERATORS.contains(operatorString))

            {

                Matrix matrix = null;

                try {

                    matrix = recentTextMatrix.multiply(getGraphicsState().getCtm());

                } catch (IllegalArgumentException e) {

                    throw new RuntimeException(e);

                }

                float y = matrix.get(Matrix.I32);

                if (triggerRectangles.stream().anyMatch(rect -> rect.getBottom() <= y && y <= rect.getTop())) {

                    if ("TJ".equals(operatorString))

                        operands.set(0, new PdfArray());

                    else

                        operands.set(operands.size() - 2, new PdfString(""));

                }

            }


            super.write(processor, operator, operands);

        }


        final List<String> TEXT_SHOWING_OPERATORS = Arrays.asList("Tj", "'", "\"", "TJ");

        final Field textMatrixField;

        Matrix recentTextMatrix;

    };


    for (int i = 1; i <= pdfDocument.getNumberOfPages(); i++)

    {

        PdfPage page = pdfDocument.getPage(i);

        Set<PdfName> xobjectNames = page.getResources().getResourceNames(PdfName.XObject);

        for (PdfName xobjectName : xobjectNames) {

            PdfFormXObject xobject = page.getResources().getForm(xobjectName);

            byte[] content = xobject.getPdfObject().getBytes();

            PdfResources resources = xobject.getResources();


            RegexBasedLocationExtractionStrategy regexLocator = new RegexBasedLocationExtractionStrategy("Created by:|Calendar:");

            new PdfCanvasProcessor(regexLocator).processContent(content, resources);

            triggerRectangles.clear();

            triggerRectangles.addAll(regexLocator.getResultantLocations().stream().map(loc -> loc.getRectangle()).collect(Collectors.toSet()));


            PdfCanvas pdfCanvas = new PdfCanvas(new PdfStream(), resources, pdfDocument);

            editor.editContent(content, resources, pdfCanvas);

            xobject.getPdfObject().setData(pdfCanvas.getContentStream().getBytes());

        }

    }

}

(EditPageContent測(cè)試testRemoveSpecificLinesCalendar)


請(qǐng)注意,這是一個(gè)概念驗(yàn)證,它是為 OP 的用例特別定制的:PdfCanvasEditor此處僅用于檢查和編輯每個(gè)頁(yè)面的第一級(jí)表單 XObjects,因?yàn)閺?Google 日歷以 Agenda 格式創(chuàng)建的 PDF 包含他們所有的頁(yè)面內(nèi)容都以 XObject 形式呈現(xiàn),而 XObject 又會(huì)在頁(yè)面內(nèi)容流中繪制。此外,預(yù)計(jì)文本將與頁(yè)面頂部平行。


查看完整回答
反對(duì) 回復(fù) 2021-06-30
  • 1 回答
  • 0 關(guān)注
  • 304 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)