ITextSharp PdfCopy使用示例

时间:2010-11-25 10:27:46

标签: c# asp.net pdf-generation itextsharp

我正在尝试使用 ItextSharp 中的 PdfSmartCopy ,但我在c#中找不到任何相关的示例。

这就是我有一个包含表单字段的pdf,字段将 700kb 添加到pdf文档的大小。没有表单字段的原始文档是 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();

3 个答案:

答案 0 :(得分:7)

这是使用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

答案 1 :(得分:1)

在致电reader.removeUnusedObjects()之前致电pst.close() ...无需展平。

要缩小一些东西,你可以pst.setFullCompression()。 YMMV。

编辑:就例子而言,我建议将iText付诸行动,第2版。那里有各种各样的例子,包括PdfCopy&amp; PdfSmartCopy。本书中的所有代码示例均为available on line

如果您购买这本书,我不会赚钱,但是通过众多在线互动了解作者,并认为他是朋友。

答案 2 :(得分:0)

&#13;
&#13;
using iTextSharp.text;
using iTextSharp.text.pdf;

public void pdfcopyfile()
    {
        string pdfTemplatePath = @"D:\1.pdf";
        string outputPdfPath = @"D:\44.pdf";
        iTextSharp.text.pdf.PdfReader reader = null;
        int pageCount = 0;
        int currentPage = 0;
        Document pdfDoc = null;
        PdfCopy writer = null;
        PdfImportedPage page = null;
        reader = new PdfReader(pdfTemplatePath);
        pdfDoc = new Document(reader.GetPageSizeWithRotation(1));
        writer = new PdfCopy(pdfDoc, new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
        pageCount = reader.NumberOfPages;
        pdfDoc.Open();
        while (currentPage < pageCount)
        {
            currentPage += 1;
            pdfDoc.SetPageSize(reader.GetPageSizeWithRotation(currentPage));
            pdfDoc.NewPage();
            page = writer.GetImportedPage(reader, currentPage);
            writer.AddPage(page);
        }
        reader = new PdfReader(pdfTemplatePath);
        pageCount = reader.NumberOfPages;
        currentPage = 0;
        pdfDoc.Close();
    }
&#13;
&#13;
&#13;

相关问题