我试图从ItextSharp使用PdfSmartCopy ,但我无法在c#中找到任何相关示例。
理念是我有一个包含表单字段的pdf,并且这些字段为pdf文档的大小添加了700kb 。 没有表单域的原始文件是100kb 。 欢迎任何其他提示,尤其是o一致缩小pdf大小。
(我使用Adobe Acrobat对生成的PDF进行了优化,并将其缩减为44kb ,因此必须在某处出现故障。)有什么方法可以减小PDF大小?
编辑:FormFlatenning没有帮助。 pdf模板文件仅包含文本,行和表格,不包含图像。
这是我的代码片段
PdfReader reader = new PdfReader(GetTemplateBytes()); pst = new PdfStamper(reader, Response.OutputStream); var acroFields = pst.AcroFields; pst.FormFlattening = true; pst.FreeTextFlattening = true; SetFieldsInternal(acroFields); pst.Close();I am trying to use PdfSmartCopy from ItextSharp but I cannot find any relevant examples in c#.
The ideea is that I have a pdf containing form fields and the fields add 700kb to the size of the pdf document. The original document without form fields was 100kb. Any other sugestions are welcome, especially o reduce the pdf size consistently.
(I optimised the generated PDF with adobe acrobat, and it reduced it to 44kb. So there must be a glitch somewhere.) Is there any way to reduce the PDF size?
Edit: FormFlatenning doesn't help. The pdf template file contains only text, lines and tables, no images.
here's my code snippet
PdfReader reader = new PdfReader(GetTemplateBytes()); pst = new PdfStamper(reader, Response.OutputStream); var acroFields = pst.AcroFields; pst.FormFlattening = true; pst.FreeTextFlattening = true; SetFieldsInternal(acroFields); pst.Close();最满意答案
这是一个使用ITextSharp PDFCopy将多个PDF文件复制到1个多页PDF文件的2008 VB.Net示例。 这将复制除底层链接外的所有内容。 它似乎完全复制所有注释,至少我找不到它没有复制。
注意:您必须在项目中引用ITextSharp。
输入参数:
fileArray - 一个pdf文件数组。
outPutPDF - 完整路径和名称以输出多页PDF文档。
Private Sub BuildMultiPagePDF(ByVal fileArray As String(), ByVal outPutPDF As String) Try Dim reader As iTextSharp.text.pdf.PdfReader = Nothing Dim pageCount As Integer = 0 Dim currentPage As Integer = 0 Dim pdfDoc As iTextSharp.text.Document = Nothing Dim writer As iTextSharp.text.pdf.PdfCopy = Nothing Dim page As iTextSharp.text.pdf.PdfImportedPage = Nothing Dim currentPDF As Integer = 0 If fileArray.Length > 0 Then reader = New iTextSharp.text.pdf.PdfReader(fileArray(currentPDF)) pdfDoc = New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1)) writer = New iTextSharp.text.pdf.PdfCopy(pdfDoc, _ New IO.FileStream(outPutPDF, _ IO.FileMode.OpenOrCreate, _ IO.FileAccess.Write)) pageCount = reader.NumberOfPages While currentPDF < fileArray.Length pdfDoc.Open() While currentPage < pageCount currentPage += 1 pdfDoc.SetPageSize(reader.GetPageSizeWithRotation(currentPage)) pdfDoc.NewPage() page = writer.GetImportedPage(reader, currentPage) writer.AddPage(page) End While currentPDF += 1 If currentPDF < fileArray.Length Then reader = New iTextSharp.text.pdf.PdfReader(fileArray(currentPDF)) pageCount = reader.NumberOfPages currentPage = 0 End If End While pdfDoc.Close() Else MessageBox.Show("The input file array is empty. Processing terminated.", _ "INVALID FILE LIST", _ MessageBoxButtons.OK, MessageBoxIcon.Error) End If Catch ex As Exception MessageBox.Show(ex.message) End Try End SubHere is a 2008 VB.Net example of using ITextSharp PDFCopy to copy multiple PDF files into 1 multi-page PDF file. This will copy everything except underlying links. It appears to copy all annotations perfectly, at least I could not find any it did not copy.
Note: You must have ITextSharp referenced in your project.
Input Parameters:
fileArray – an array of pdf files.
outPutPDF – full path and name to output multipage PDF document.
Private Sub BuildMultiPagePDF(ByVal fileArray As String(), ByVal outPutPDF As String) Try Dim reader As iTextSharp.text.pdf.PdfReader = Nothing Dim pageCount As Integer = 0 Dim currentPage As Integer = 0 Dim pdfDoc As iTextSharp.text.Document = Nothing Dim writer As iTextSharp.text.pdf.PdfCopy = Nothing Dim page As iTextSharp.text.pdf.PdfImportedPage = Nothing Dim currentPDF As Integer = 0 If fileArray.Length > 0 Then reader = New iTextSharp.text.pdf.PdfReader(fileArray(currentPDF)) pdfDoc = New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1)) writer = New iTextSharp.text.pdf.PdfCopy(pdfDoc, _ New IO.FileStream(outPutPDF, _ IO.FileMode.OpenOrCreate, _ IO.FileAccess.Write)) pageCount = reader.NumberOfPages While currentPDF < fileArray.Length pdfDoc.Open() While currentPage < pageCount currentPage += 1 pdfDoc.SetPageSize(reader.GetPageSizeWithRotation(currentPage)) pdfDoc.NewPage() page = writer.GetImportedPage(reader, currentPage) writer.AddPage(page) End While currentPDF += 1 If currentPDF < fileArray.Length Then reader = New iTextSharp.text.pdf.PdfReader(fileArray(currentPDF)) pageCount = reader.NumberOfPages currentPage = 0 End If End While pdfDoc.Close() Else MessageBox.Show("The input file array is empty. Processing terminated.", _ "INVALID FILE LIST", _ MessageBoxButtons.OK, MessageBoxIcon.Error) End If Catch ex As Exception MessageBox.Show(ex.message) End Try End Sub更多推荐
发布评论