iTextSharp的PdfSmartCopy类的编辑DirectContent [英] Edit DirectContent of iTextSharp PdfSmartCopy class
问题描述
在我的工作有时我不得不从几个合并几百PDF文件。我一直在使用作家
和 ImportedPages
类的所有时光。但是,当我已经合并所有文件合并成一个,文件大小变得很大,所有的总和合并的文件大小,因为字体被连接到每一个页面,而不是重复使用(字体嵌入到每一个网页,而不是整个文档)。
不是很久以前我发现了 PdfSmartCopy
类,它重用嵌入字体和图像。这里的问题踢。很多时候,合并文件一起之前,我必须更多的内容添加到它们(图片,文字)。为此,我通常使用 PdfContentByte
从作家
对象。
文档的文档=新的文件();
PdfWriter作家= PdfWriter.GetInstance(文件,新的FileStream(C:\test.pdf,FileMode.Create));
PdfContentByte CB = writer.DirectContent;
cb.Rectangle(100,100,100,100);
cb.SetColorStroke(BaseColor.RED);
cb.SetColorFill(BaseColor.RED);
cb.FillStroke();
当我做 PdfSmartCopy
类似的事情对象,页面被合并,但不添加任何额外的内容。与 PdfSmartCopy
我测试的全码:使用
(文档DOC =新的文档())
{
使用(PdfSmartCopy副本=新PdfSmartCopy(文件,新的FileStream(Path.GetDirectoryName(pdfPath [0])+\\testas.pdf,FileMode.Create )))
{
doc.Open();
PdfContentByte CB = copy.DirectContent;
的for(int i = 0; I< pdfPath.Length;我++)
{
PdfReader读卡器=新PdfReader(pdfPath [I]);
表示(中间体二= 0; II蛋白酶reader.NumberOfPages;ⅱ++)
{
PdfImportedPage进口= copy.GetImportedPage(读者,ⅱ+ 1);
copy.AddPage(进口);
cb.Rectangle(100,100,100,100);
cb.SetColorStroke(BaseColor.RED);
cb.SetColorFill(BaseColor.RED);
cb.FillStroke();
doc.NewPage(); //净nesessary线
// ColumnText山坳=新ColumnText(CB);
//col.SetSimpleColumn(100,100,500,500);
//col.AddText(new块(wdasdasd,PdfFontManager.GetFont(@C:\Windows\Fonts\arial.ttf,20)));
//col.Go();
}
}
}
}
}
现在我有几个问题:
- 是否有可能修改
PdfSmartCopy
对象的DirectContent? - 如果不是,有另一种方式将多个PDF文件合并成一个没有显着增加它的大小和仍然能够更多的内容添加到页面,同时合并?
首先这样的:使用 PdfWriter
/ PdfImportedPage
是不是一个好主意。你扔掉所有的互动功能!作为iText的笔者,这是非常令人沮丧这么多人做,尽管我写这两部书的事实,同样的错误,尽管我相信我的出版商提供的最重要的一章的事实免费: http://www.manning.com/lowagie2/samplechapter6.pdf
是我写的真的那么糟吗?还是有另一种人之所以继续使用 PdfWriter
/ PdfImportedPage
?
合并文件
至于你的具体问题,这里有答案:
- 是。下载样章,并搜索
PageStamp
的PDF文件。 - 只有当你创建两道的PDF。例如:首先创建PDF巨大,那么将其通过
PdfCopy
减少尺寸;或创建合并的PDF先用PdfCopy,然后在第二次使用添加额外的内容PdfStamper
。
At my work sometimes I have to merge from few to few hundreds pdf files. All the time I've been using Writer
and ImportedPages
classes. But when I have merged all files into one, file size becomes enormous, sum of all merged files sizes, because fonts being attached to every page, and not reused (fonts are embedded to every page, not whole document).
Not very long time ago I found out about PdfSmartCopy
class, which reuses embedded fonts and images. And here the problem kicks in. Very often, before merging files together, I have to add additional content to them (images, text). For this purpose I usually use PdfContentByte
from Writer
object.
Document doc = new Document();
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("C:\test.pdf", FileMode.Create));
PdfContentByte cb = writer.DirectContent;
cb.Rectangle(100, 100, 100, 100);
cb.SetColorStroke(BaseColor.RED);
cb.SetColorFill(BaseColor.RED);
cb.FillStroke();
When I do similar thing with PdfSmartCopy
object, pages are merged, but no additional content being added. Full code of my test with PdfSmartCopy
:
using (Document doc = new Document())
{
using (PdfSmartCopy copy = new PdfSmartCopy(doc, new FileStream(Path.GetDirectoryName(pdfPath[0]) + "\\testas.pdf", FileMode.Create)))
{
doc.Open();
PdfContentByte cb = copy.DirectContent;
for (int i = 0; i < pdfPath.Length; i++)
{
PdfReader reader = new PdfReader(pdfPath[i]);
for (int ii = 0; ii < reader.NumberOfPages; ii++)
{
PdfImportedPage import = copy.GetImportedPage(reader, ii + 1);
copy.AddPage(import);
cb.Rectangle(100, 100, 100, 100);
cb.SetColorStroke(BaseColor.RED);
cb.SetColorFill(BaseColor.RED);
cb.FillStroke();
doc.NewPage();// net nesessary line
//ColumnText col = new ColumnText(cb);
//col.SetSimpleColumn(100,100,500,500);
//col.AddText(new Chunk("wdasdasd", PdfFontManager.GetFont(@"C:\Windows\Fonts\arial.ttf", 20)));
//col.Go();
}
}
}
}
}
Now I have few questions:
- Is it possible to edit
PdfSmartCopy
object's DirectContent? - If not, is there another way to merge multiple pdf files into one not increasing its size dramatically and still being able to add additional content to pages while merging?
First this: using PdfWriter
/PdfImportedPage
is not a good idea. You throw away all interactive features! Being the author of iText, it's very frustrating to so many people making the same mistake in spite of the fact that I wrote two books about this, and in spite of the fact that I convinced my publisher to offer one of the most important chapters for free: http://www.manning.com/lowagie2/samplechapter6.pdf
Is my writing really that bad? Or is there another reason why people keep on merging documents using PdfWriter
/PdfImportedPage
?
As for your specific questions, here are the answers:
- Yes. Download the sample chapter and search the PDF file for
PageStamp
. - Only if you create the PDF in two passes. For instance: create the huge PDF first, then reduce the size by passing it through
PdfCopy
; or create the merged PDF first with PdfCopy, then add the extra content in a second pass usingPdfStamper
.
这篇关于iTextSharp的PdfSmartCopy类的编辑DirectContent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!