Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

c# - ITextSharp PdfCopy use examples

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();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...