为什么要将数据添加到 PDF 内容流中?

Why is data added to the PDF content stream?

使用此代码时 (Removing Watermark from PDF iTextSharp) to simply read and re-write the content stream for an identical PDF, I get additional operations added to the content stream for this 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 class 只是所有内容操作的集合,其中一些标记为删除。

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 派生的 class,就像 @mkl 示例中的 TransparentGraphicsRemover。

这个加法

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

旋转中间的所有内容。添加这是 iText(Sharp) 的 'service' 旨在允许您忽略旋转并使用更自然的坐标绘制内容。

不幸的是,这项服务对于手头的任务没有意义。因此,您应该将其关闭。

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;
    }
} 

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

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