iTextSharp的PdfCopy使用示例 [英] ITextSharp PdfCopy use examples

查看:622
本文介绍了iTextSharp的PdfCopy使用示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 PdfSmartCopy iTextSharp的,但我找不到在C#中的任何相关的例子。

该IDEEA的是,我有一个包含表单字段的PDF和字段添加 700KB 以PDF文件的大小。没有表单域原始文档的 100KB
任何其他sugestions的欢迎,特别是运行减少PDF文件大小一致。

(我最优化使用Adobe Acrobat生成的PDF,而且它降低到 44KB ,所以必须有毛刺的地方。)
有什么办法来减少PDF的大小?

编辑:FormFlatenning于事无补。 PDF模板文件只包含文字,线条和表格,没有图像。

这是我的code段

  PdfReader读卡器=新PdfReader(GetTemplateBytes());
        PST =新PdfStamper(读卡器,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文档

 私人小组BuildMultiPagePDF(BYVAL fileArray作为字符串(),BYVAL outPutPDF作为字符串)
    尝试        昏暗的读者作为iTextSharp.text.pdf.PdfReader =无
        昏暗页页次为整数= 0
        当前是昏暗为整数= 0
        昏暗pdfDoc作为iTextSharp.text.Document =无
        昏暗的作家作为iTextSharp.text.pdf.PdfCopy =无
        昏暗的页面作为iTextSharp.text.pdf.PdfImportedPage =无
        昏暗currentPDF为整数= 0        如果fileArray.Length> 0,则            读者=新iTextSharp.text.pdf.PdfReader(fileArray(currentPDF))
            pdfDoc =新iTextSharp.text.Document(reader.GetPageSizeWithRotation(1))
            作家=新iTextSharp.text.pdf.PdfCopy(pdfDoc,_
                                                  新IO.FileStream(outPutPDF,_
                                                  IO.FileMode.OpenOrCreate,_
                                                  IO.FileAccess.Write))            = PAGECOUNT reader.NumberOfPages            虽然currentPDF< fileArray.Length
                pdfDoc.Open()                虽然当前是<页页次
                    当前是+ = 1
                    pdfDoc.SetPageSize(reader.GetPageSizeWithRotation(当前页))
                    pdfDoc.NewPage()
                    页= writer.GetImportedPage(读卡器,当前页)
                    writer.AddPage(页)
                虽然结束                currentPDF + = 1
                如果currentPDF< fileArray.Length然后
                    读者=新iTextSharp.text.pdf.PdfReader(fileArray(currentPDF))
                    = PAGECOUNT reader.NumberOfPages
                    当前页= 0
                万一
            虽然结束            pdfDoc.Close()
        其他
            MessageBox.Show(输入文件数组为空。处理终止。_
                            无效文件列表,_
                            MessageBoxButtons.OK,MessageBoxIcon.Error)        万一    抓住EX为例外
        MessageBox.Show(ex.message)
    结束Try
结束小组

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();

解决方案

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

这篇关于iTextSharp的PdfCopy使用示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆