Using PdfCleanUpTool or PdfAutoSweep causes some text to change to bold, line weights increase and double points change to hearts

huangapple go评论84阅读模式
英文:

Using PdfCleanUpTool or PdfAutoSweep causes some text to change to bold, line weights increase and double points change to hearts

问题

以下是您提供的内容的翻译:

我在尝试使用iText 7时遇到了奇怪的问题。 PDF的某些部分被修改了(文本变为粗体,线条加粗,双点变为心形)。 在iText 5.4.4版本中没有发生这种情况,但是自那以后的每个版本(5或7)都会导致相同的问题。

有人知道为什么会发生这种情况吗?是否有任何方法可以绕过这个问题? 任何帮助将不胜感激!

如果需要更多信息,我会尝试提供。

以下是我用来测试iText 7的简单代码。
示例PDF文件

package javaapplication1;

import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.pdfcleanup.PdfCleanUpLocation;
import com.itextpdf.pdfcleanup.PdfCleanUpTool;
import com.itextpdf.pdfcleanup.autosweep.ICleanupStrategy;
import com.itextpdf.pdfcleanup.autosweep.PdfAutoSweep;
import com.itextpdf.pdfcleanup.autosweep.RegexBasedCleanupStrategy;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;


public class JavaApplication1 {
    
    public static final String DEST = "D:/TEMP/TEMP/PDF/result/orientation_result.pdf";
    public static final String DEST2 = "D:/TEMP/TEMP/PDF/result/orientation_result2.pdf";
    public static final String DEST3 = "D:/TEMP/TEMP/PDF/result/orientation_result3.pdf";
    public static final String SRC = "D:/TEMP/TEMP/PDF/TEST_PDF.pdf";

    public static void main(String[] args) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new JavaApplication1().manipulatePdf(DEST);
        new JavaApplication1().manipulatePdf2(DEST2);
        
        try (PdfDocument pdf = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST3))) {
            final ICleanupStrategy cleanupStrategy = new RegexBasedCleanupStrategy(Pattern.compile("2019", Pattern.CASE_INSENSITIVE)).setRedactionColor(ColorConstants.PINK);
            final PdfAutoSweep autoSweep = new PdfAutoSweep(cleanupStrategy);
            autoSweep.cleanUp(pdf);
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

    protected void manipulatePdf(String dest) throws IOException {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));

        List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<PdfCleanUpLocation>();

        // The arguments of the PdfCleanUpLocation constructor: the number of page to be cleaned up,
        // a Rectangle defining the area on the page we want to clean up,
        // a color which will be used while filling the cleaned area.
        PdfCleanUpLocation location = new PdfCleanUpLocation(1, new Rectangle(97, 405, 383, 40),
                ColorConstants.GRAY);
        cleanUpLocations.add(location);

        PdfCleanUpTool cleaner = new PdfCleanUpTool(pdfDoc, cleanUpLocations);
        cleaner.cleanUp();

        pdfDoc.close();
    }
    
    protected void manipulatePdf2(String dest) throws IOException {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));

        // If the second argument is true, then regions to be erased are extracted from the redact annotations
        // contained inside the given document. If the second argument is false (that's default behavior),
        // then use PdfCleanUpTool.addCleanupLocation(PdfCleanUpLocation)
        // method to set regions to be erased from the document.
        PdfCleanUpTool cleaner = new PdfCleanUpTool(pdfDoc, true);
        cleaner.cleanUp();

        pdfDoc.close();
    }
}
英文:

I have weird problem when I try to use iText 7. Some parts of the PDF are modified (text to change to bold, line weights increase and double points change to hearts). In iText version 5.4.4 this didn't happened, but every version since that I have tried cause this same problem (5 or 7).

Does anyone have a clued why this is happening and is there anything I could to do to bypass this problem? Any help would be appreciated!

If more information is needed, I will try to provide it.

Using PdfCleanUpTool or PdfAutoSweep causes some text to change to bold, line weights increase and double points change to hearts

Using PdfCleanUpTool or PdfAutoSweep causes some text to change to bold, line weights increase and double points change to hearts

Below is simple code that I used to test iText 7.
Example PDF Files

package javaapplication1;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.pdfcleanup.PdfCleanUpLocation;
import com.itextpdf.pdfcleanup.PdfCleanUpTool;
import com.itextpdf.pdfcleanup.autosweep.ICleanupStrategy;
import com.itextpdf.pdfcleanup.autosweep.PdfAutoSweep;
import com.itextpdf.pdfcleanup.autosweep.RegexBasedCleanupStrategy;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
public class JavaApplication1 {
public static final String DEST = &quot;D:/TEMP/TEMP/PDF/result/orientation_result.pdf&quot;;
public static final String DEST2 = &quot;D:/TEMP/TEMP/PDF/result/orientation_result2.pdf&quot;;
public static final String DEST3 = &quot;D:/TEMP/TEMP/PDF/result/orientation_result3.pdf&quot;;
public static final String SRC = &quot;D:/TEMP/TEMP/PDF/TEST_PDF.pdf&quot;;
public static void main(String[] args) throws IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new JavaApplication1().manipulatePdf(DEST);
new JavaApplication1().manipulatePdf2(DEST2);
try (PdfDocument pdf = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST3))) {
final ICleanupStrategy cleanupStrategy = new RegexBasedCleanupStrategy(Pattern.compile(&quot;2019&quot;, Pattern.CASE_INSENSITIVE)).setRedactionColor(ColorConstants.PINK);
final PdfAutoSweep autoSweep = new PdfAutoSweep(cleanupStrategy);
autoSweep.cleanUp(pdf);
} catch (Exception e) {
System.out.println(e.toString());
}
}
protected void manipulatePdf(String dest) throws IOException {
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
List&lt;PdfCleanUpLocation&gt; cleanUpLocations = new ArrayList&lt;PdfCleanUpLocation&gt;();
// The arguments of the PdfCleanUpLocation constructor: the number of page to be cleaned up,
// a Rectangle defining the area on the page we want to clean up,
// a color which will be used while filling the cleaned area.
PdfCleanUpLocation location = new PdfCleanUpLocation(1, new Rectangle(97, 405, 383, 40),
ColorConstants.GRAY);
cleanUpLocations.add(location);
PdfCleanUpTool cleaner = new PdfCleanUpTool(pdfDoc, cleanUpLocations);
cleaner.cleanUp();
pdfDoc.close();
}
protected void manipulatePdf2(String dest) throws IOException {
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
// If the second argument is true, then regions to be erased are extracted from the redact annotations
// contained inside the given document. If the second argument is false (that&#39;s default behavior),
// then use PdfCleanUpTool.addCleanupLocation(PdfCleanUpLocation)
// method to set regions to be erased from the document.
PdfCleanUpTool cleaner = new PdfCleanUpTool(pdfDoc, true);
cleaner.cleanUp();
pdfDoc.close();
}
}

答案1

得分: 0

抱歉回复晚了。我现在已经验证,更新到最新版本可以解决这个问题。感谢 @mkl 指出了这一点。

问题已解决。

英文:

Sorry for late response. I managed now verify that updating to latest versions corrected this problem. Thanks to @mkl pointing this out.

Problem solved

huangapple
  • 本文由 发表于 2020年10月21日 19:31:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64462607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定