卸下單獨的圖形對象可能會變得有些棘手,但它不是不可能的。最難的部分是決定要刪除哪些對象。下面是一些示例代碼靶向iTextSharp的4.1.6,首先創建一個帶有兩個矩形組成的PDF,然後創建基於第一移除了矩形之一的第二PDF。你需要運用你的邏輯來找出你想要刪除的矩形。有可能你實際上沒有矩形,而是直線形成一個矩形,在這種情況下,你也需要修改一下代碼。
這第一位剛在桌面上創建一個基本的PDF與兩個矩形:
//Create a file on the desktop with two rectangles
var file1 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "File1.pdf");
using (var fs = new FileStream(file1, FileMode.Create, FileAccess.Write, FileShare.None)) {
var doc = new Document();
var writer = PdfWriter.GetInstance(doc, fs);
doc.Open();
var cb = writer.DirectContent;
//Draw two rectangles
cb.SaveState();
cb.SetColorStroke(iTextSharp.text.Color.RED);
cb.Rectangle(40, 60, 200, 100);
cb.Stroke();
cb.RestoreState();
cb.SaveState();
cb.SetColorStroke(iTextSharp.text.Color.BLUE);
cb.Rectangle(500, 80, 90, 50);
cb.Stroke();
cb.RestoreState();
doc.Close();
}
接下來的這個部分是更加複雜的部分。我鼓勵你做一個Console.WriteLine(tokenizer.StringValue);
內while
循環的看到所有的PDF命令。你會注意到他們使用RPN syntax這可能需要一點時間才能使用。有關更多問題,請參閱代碼中的註釋。
//Bind a reader to our first file
var reader = new PdfReader(file1);
//Get the first page (this would normally be done in a loop)
var page = reader.GetPageN(1);
//Get the "contents" of that page
var objectReference = (PdfIndirectReference)page.Get(PdfName.CONTENTS);
//Get the actual stream of the "contents"
var stream = (PRStream)PdfReader.GetPdfObject(objectReference);
//Get the raw bytes of the stream
var streamBytes = PdfReader.GetStreamBytes(stream);
//Convert the bytes to actual PDF tokens/commands
var tokenizer = new PRTokeniser(new RandomAccessFileOrArray(streamBytes));
//We're going to re-append each token to this below buffer and remove the ones that we don't want
List<string> newBuf = new List<string>();
//Loop through each PDf token
while (tokenizer.NextToken()) {
//Add them to our master buffer
newBuf.Add(tokenizer.StringValue);
//The "Other" token is used for most commands, so if we're on "Other" and the current command is "re" which is rectangle
if (
tokenizer.TokenType == PRTokeniser.TK_OTHER && //The "Other" token is used for most commands
newBuf[newBuf.Count - 1] == "re" && //re is the rectangle command
newBuf[newBuf.Count - 5] == "40" //PDFs use RPN syntax so the red rectangle command was "40 60 200 100 re"
) {
newBuf.RemoveRange(newBuf.Count - 5, 5); //If the above conditions were met remove the last 5 commands
}
}
//Convert our array to a string with newlines between each token, convert that to an ASCII byte array and push that back into the stream (erasing the current contents)
stream.SetData(System.Text.Encoding.ASCII.GetBytes(String.Join("\n", newBuf.ToArray())));
//Create a new file with the rectangle removed
var file2 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "File2.pdf");
using (var fs = new FileStream(file2, FileMode.Create, FileAccess.Write, FileShare.None)) {
//Bind a stamper to our read above which has the altered stream
var stamper = new PdfStamper(reader, fs);
//Loop through each page
int total = reader.NumberOfPages;
for (int i = 1; i <= total; i++) {
//Push the content over page by page
reader.SetPageContent(i, reader.GetPageContent(i));
}
stamper.Close();
}
reader.Close();
WOW!這段代碼確實幫助我實現了我認爲幾乎不可能實現的目標。感謝一百萬克里斯! – 2013-03-26 18:39:59
@brunolau請注意,對於通用對象刪除方法,您必須識別更多的流。此外,您還必須檢查xobjects,而不僅僅是即時頁面內容流。這裏介紹的代碼只是一個啓動器。 – mkl 2013-03-27 07:11:08