ITextSharp PdfCopy使用示例(ITextSharp PdfCopy use examples)

编程入门 行业动态 更新时间:2024-10-19 21:29:20
ITextSharp PdfCopy使用示例(ITextSharp PdfCopy use examples)

我试图从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 Sub

Here 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

更多推荐

本文发布于:2023-07-16 12:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128601.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:示例   ITextSharp   PdfCopy   examples

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!