C# 从PDF iTextSharp中删除字体
我在VisualStudio2010中使用带有C#的iTextSharp,最近遇到了以下情况。我收到了几本分成许多PDF文件的电子书,这些文件的边框上有厨房标记,我使用以下代码删除了它们:C# 从PDF iTextSharp中删除字体,c#,pdf,itextsharp,itext,embedded-fonts,C#,Pdf,Itextsharp,Itext,Embedded Fonts,我在VisualStudio2010中使用带有C#的iTextSharp,最近遇到了以下情况。我收到了几本分成许多PDF文件的电子书,这些文件的边框上有厨房标记,我使用以下代码删除了它们: x = reader.GetPageSize(i).Width; y = reader.GetPageSize(i).Height; iTextSharp.text.Rectangle tRect = new iTextSharp.text.Rectangle(x - 5
x = reader.GetPageSize(i).Width;
y = reader.GetPageSize(i).Height;
iTextSharp.text.Rectangle tRect =
new iTextSharp.text.Rectangle(x - 52, y - 52);
Document document = new Document(tRect);
PdfWriter writer = PdfWriter.GetInstance(document,
new FileStream(dest, FileMode.OpenOrCreate));
document.Open();
PdfContentByte content = writer.DirectContent;
PdfImportedPage page = writer.GetImportedPage(reader, i);
content.AddTemplate(page, -offset, -offset);
document.NewPage();
document.SetMargins(0, 0, 0, 0);
document.Close();
reader.Close();
当然,这包含在一个以i为序数的For循环中。在我遍历了我正在处理的部分中的每个页面之后,我使用以下代码将它们合并在一起:
private void mergePDF(string fName, string folderPath)
{
string[] files = Directory.GetFiles(folderPath);
iTextSharp.text.Document tDoc = new iTextSharp.text.Document();
iTextSharp.text.pdf.PdfCopy copy =
new iTextSharp.text.pdf.PdfCopy(tDoc,
new FileStream(fName, FileMode.Create));
tDoc.Open();
iTextSharp.text.pdf.PdfReader reader;
int n = 0;
for (int i = 0; i < files.Length; i++)
{
reader = new iTextSharp.text.pdf.PdfReader(files[i]);
n = reader.NumberOfPages;
for (int page = 0; page < n; )
{
copy.AddPage(copy.GetImportedPage(reader, ++page));
}
copy.FreeReader(reader);
reader.Close();
}
tDoc.Close();
}
private void mergePDF(字符串fName,字符串folderPath)
{
string[]files=Directory.GetFiles(folderPath);
iTextSharp.text.Document tDoc=新的iTextSharp.text.Document();
iTextSharp.text.pdf.PdfCopy副本=
新的iTextSharp.text.pdf.PdfCopy(tDoc,
新的文件流(fName,FileMode.Create));
tDoc.Open();
iTextSharp.text.pdf.PdfReader阅读器;
int n=0;
for(int i=0;i
完成后,我发现我的文件大小增加了一倍(特别是一个文件在处理前的重量为20180KB,处理后的重量为41322KB)
我做了一些挖掘,似乎当使用iTextSharp拆分PDF时,程序会在拆分的每个PDF中嵌入完整PDF的所有字体,显然这可以占文件大小的50-80%
也就是说,有人知道使用iTextSharp从PDF中删除嵌入字体的方法吗。我的计划是只在第一个PDF文件中包含它们,然后当PDF重新编译时,文档中只有一个字体副本,我的大小将更合适
同样值得注意的是,此代码与我的实际代码非常接近-逻辑相同,但出于大小和流量考虑,添加了一些变量。好的,我找到了自己的答案。我没有使用PdfCopy,而是使用了PdfSmartCopy,在上面提到的例子中,我从处理后的41322KB增加到16854KB(原稿是20180KB),与原稿相差近3.5MB!虽然PdfSmartCopy而不是PdfCopy是删除重复流的正确方法,但删除galley标记的代码的设计不是很理想。您可以使用PdfStamper单次运行来操作文档页面的媒体框条目,而不是提取和插入整个页面内容,从而达到相同的效果。查阅或者您实际上可能只是想创建适当的裁剪框条目。。。