2016-09-06 13 views
1

このコード(Removing Watermark from PDF iTextSharp)を使用して、同じPDFのコンテンツストリームを単純に読み込んだり書き直したりすると、コンテンツストリームに追加操作が追加されます(file)。コンテンツストリームの後のコンテンツストリームの前になぜデータがPDFコンテンツストリームに追加されますか?

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は、@ MKLの例ではちょうどTransparentGraphicsRemoverのように、PdfContentStreamEditorから派生しているだけのクラスです。

答えて

3

このほか

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

の間のすべてを回転させます。これを追加することはiText(シャープ)の 'サービス'です。より自然な座標を使って回転を無視したり、素材を描画したりすることができます。

残念ながら、このサービスは、手元にあるタスクには意味がありません。したがって、あなたはそれを切り替えるべきです。

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コメントしているこのように、この二重のコメント。。)

したがって、RotateContentfalseに設定することを提案します。

+0

完全に動作します。ありがとう@mkl! – Darren

関連する問題