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
                        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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With