代码之家  ›  专栏  ›  技术社区  ›  Dragos Durlut

ITextSharp PDF复制使用示例

  •  8
  • Dragos Durlut  · 技术社区  · 15 年前

    我想用 PDF副本 伊特克斯夏普 但我在C中找不到任何相关的例子。

    IDEEA是我有一个包含表单域和字段添加的PDF 7000 kb 与PDF文档的大小相同。没有表单域的原始文档是 100kb . 欢迎任何其他建议,尤其是不断减小PDF大小。

    (我用AdobeAcrobat优化了生成的PDF,并将其简化为 44 kb . 所以肯定有点小故障。) 有没有办法减小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 回复  |  直到 8 年前
        1
  •  7
  •   Ferruccio    14 年前

    下面是一个2008 vb.net的例子,它使用itextsharp pdf copy将多个PDF文件复制到一个多页的PDF文件中。这将复制除基础链接之外的所有内容。它似乎完美地复制了所有注释,至少我找不到它没有复制的任何注释。

    注意:您的项目中必须引用ITextSharp。

    输入参数:

    文件数组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
    
        2
  •  1
  •   Mark Storer    15 年前

    呼叫 reader.removeUnusedObjects() 呼叫前 pst.close() …不需要压平。

    再缩小一点 pst.setFullCompression() . YMMV。

    编辑:就示例而言,我建议使用IText,第2版。这里有很多各种各样的例子,包括pdfcopy和pdfsmartcopy。书中所有的代码示例都是 available on line .

    如果你买了这本书,我一点也不赚钱,但我通过无数的网上互动了解了作者,并认为他是我的朋友。

        3
  •  0
  •   sakthi sudhan    8 年前

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