有个pdf文件,不管是用福昕,还是adobe,都无法正常删除水印。
用 Patagames 库,去掉它
核心代码 :
using Patagames.Pdf;
using Patagames.Pdf.Net;
private void pdfHandler(string name) {
string path = name.Substring(0, name.LastIndexOf('\\') + 1);
string[] fileNameExt = name.Substring(name.LastIndexOf('\\') + 1).Split('.');
string newName = path + fileNameExt[0] + "_"+DateTime.Now.ToString("yyyy_MM_dd-HHmmss") + "." + fileNameExt[1];
PdfDocument document = PdfDocument.Load(name);
for(int i = 0; i < document.Pages.Count; i++) {
PdfPage collection = document.Pages[i];
Console.WriteLine(collection.Text);
FS_SIZEF pageSize = document.GetPageSizeByIndex(i);
//Image image = new Image();
Console.WriteLine("page: " + i + ", " + collection.PageObjects.Count);
int removeCount = 0;
for(int j = collection.PageObjects.Count - 1; j >= 0; j--) {
FS_RECTF rec = collection.PageObjects[j].BoundingBox;
float area = rec.Height * rec.Width;
if(isFilter((int)area)) {
removeCount++;
Console.WriteLine(string.Format("pages: {0}, RemoveAt Ojbect: {1} , area : {2}", i, j, area));
collection.PageObjects.RemoveAt(j);
}
//Console.WriteLine(string.Format("pages: {0}, Ojbect: {1} , area : {2}", i, j, area));
}
if(removeCount != 2) {
Console.WriteLine(string.Format("pages: {0} error", removeCount));
}
collection.GenerateContent();
}
document.WriteBlock += (s, ex) => {
using(var stream = new FileStream(newName, FileMode.OpenOrCreate, FileAccess.ReadWrite)) {
stream.Seek(0, SeekOrigin.End);
stream.Write(ex.Buffer, 0, ex.Buffer.Length);
}
};
document.Save(Patagames.Pdf.Enums.SaveFlags.NoIncremental | Patagames.Pdf.Enums.SaveFlags.RemoveUnusedObjects);
document.Dispose();
}
private void Form_Load(object sender, EventArgs e) {
// regedit web https://patagames.com/request-trial/
// how to use the license
//Initialize the SDK library with license key
//You have to call this function before you can call any PDF processing functions.
PdfCommon.Initialize("04040B50*********");
//Open and load a PDF document from a file.
}