【问题标题】:Why is data added to the PDF content stream?为什么要向 PDF 内容流添加数据?
【发布时间】:2016-09-06 14:07:42
【问题描述】:

当使用此代码 (Removing Watermark from PDF iTextSharp) 简单地读取和重写相同 PDF 的内容流时,我会在此 file 的内容流中添加额外的操作。

内容流之前

q
 q
/I0 Do
Q

Q
 q
10 0 0 10 0 0 cm
0.1 0 0 0.1 0 0 cm
/QuickPDFXO6d1c5c37 Do
Q

内容流之后

q
0 -1 1 0 0 1224 cm
q
q
/I0 Do
Q
Q
q
10 0 0 10 0 0 cm
0.1 0 0 0.1 0 0 cm
/QuickPDFXO6d1c5c37 Do
Q
Q

知道为什么将它附加到我的内容流中吗?

q
0 -1 1 0 0 1224 cm
....
Q

我的代码与链接的文章类似,只是我试图从内容流中删除某些项目。

XObjectRemover editor = new XObjectRemover();
List<List<PdfContentData>> output = editor.EditPageContent(stamper, pgNumber);
PdfContentByte content = stamper.GetUnderContent(pgNumber);

foreach (List<PdfContentData> bracketList in output)
{
    foreach (PdfContentData operandList in bracketList)
    {
        if (operandList.operandToDelete == false)
        {
            int index = 0;
            foreach (PdfObject op in operandList.pdfOperands)
            {
                op.ToPdf(content.PdfWriter, content.InternalBuffer);
                content.InternalBuffer.Append(operandList.pdfOperands.Count > ++index ? (byte)' ' : (byte)'\n');
            }
        }
    }
}

PdfContentData 类只是所有内容操作的集合,其中一些标记为删除。

public class PdfContentData
{
    public int opNumber { get; set; }
    public PdfLiteral pdfOperator { get; set; }
    public List<PdfObject> pdfOperands { get; set; }
    public bool operandToDelete { get; set; }

    public PdfContentData(int opNum, PdfLiteral op, List<PdfObject> ops)
    {
        this.opNumber = opNum;
        this.pdfOperator = op;
        this.pdfOperands = ops;
    }

    public override string ToString()
    {
        return $"Ops: [{string.Join(",", pdfOperands.Select(p => p.ToString()).ToArray())}]   Del: [{operandToDelete}]";
    }
}

而 XObjectRemover 只是一个派生自 PdfContentStreamEditor 的类,就像 @mkl 示例中的 TransparentGraphicsRemover 一样。

【问题讨论】:

    标签: pdf itext


    【解决方案1】:

    这个补充

    q
    0 -1 1 0 0 1224 cm
    ....
    Q
    

    在两者之间旋转所有内容。添加这是 iText(Sharp) 的“服务”,旨在让您忽略旋转并使用更自然的坐标绘制内容。

    不幸的是,这项服务对手头的任务没有意义。因此,您应该切换它。

    PdfStamper 有一个标志,允许您这样做:

    /** Checks if the content is automatically adjusted to compensate
     * the original page rotation.
     * @return the auto-rotation status
     */    
    /** Flags the content to be automatically adjusted to compensate
     * the original page rotation. The default is <CODE>true</CODE>.
     * @param rotateContents <CODE>true</CODE> to set auto-rotation, <CODE>false</CODE>
     * otherwise
     */    
    virtual public bool RotateContents {
        set {
            stamper.RotateContents = value;
        }
        get {
            return stamper.RotateContents;
        }
    } 
    

    (这些 cmets 是 Javadoc cmets,最初与该属性的单独 getter 和 setter 相关联。因此,这个双重注释。)

    因此,我建议将RotateContent 设置为false

    【讨论】:

    • 完美运行。谢谢@mkl!
    猜你喜欢
    • 2021-08-14
    • 1970-01-01
    • 2016-09-23
    • 1970-01-01
    • 2021-07-02
    • 2018-12-12
    • 1970-01-01
    • 2012-04-12
    • 2017-07-09
    相关资源
    最近更新 更多