ITextSharp PdfCopy 使用示例

发布于 2024-10-04 04:26:40 字数 730 浏览 0 评论 0原文

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

我的想法是,我有一个包含表单字段的 pdf,并且这些字段将 700kb 添加到 pdf 文档的大小。没有表单字段的原始文档为 100kb。 欢迎任何其他建议,尤其是持续减小 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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

断念 2024-10-11 04:26:40

下面是一个 2008 VB.Net 示例,使用 ITextSharp PDFCopy 将多个 PDF 文件复制到 1 个多页 PDF 文件中。这将复制除底层链接之外的所有内容。它似乎完美地复制了所有注释,至少我找不到任何它没有复制的注释。

注意:您的项目中必须引用 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
时间海 2024-10-11 04:26:40

在调用 pst.close() 之前调用 reader.removeUnusedObjects()...无需展平。

要缩小更多内容,您可以使用 pst.setFullCompression()。 YMMV。

编辑:就示例而言,我建议使用 iText in Action,第二版。那里有很多各种各样的例子,包括 PdfCopy 和 PdfCopy。 PDF智能复制。本书中的所有代码示例都可以在线获取

买书我不挣钱,但通过无数次网上互动认识了作者,并把他当朋友。

Call reader.removeUnusedObjects() before calling pst.close()... no need for flattening.

To shrink things a little more you can pst.setFullCompression(). YMMV.

EDIT: As far as examples goes, I recommend getting iText in Action, 2nd edition. Lots of examples of all sorts of things in there, including PdfCopy & PdfSmartCopy. All the code samples from the book are available on line.

I don't make any money if you buy the book, but know the author from numerous online interactions, and consider him a friend.

岁月静好 2024-10-11 04:26:40
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();
    }

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文