2016-09-06 50 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僅僅是從PdfContentStreamEditor衍生,就像TransparentGraphicsRemover在@ MKL的例子類。

回答

3

這除了

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

(。這些評論javadoc註釋最初獨立的getter和setter此屬性。因此,這雙評論相關聯)

因此,我會建議將RotateContent設置爲false

+0

完美地工作。謝謝@mkl! – Darren