【发布时间】: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 一样。
【问题讨论】: