Add an existing PDF from a file to an unwritten document using iTextSharp

How to correctly add pages from an existing PDF file on disk to a currently created PDF document?

We have a class that creates a PDF using iTextSharp. It works great. We are currently adding the Terms and Conditions as the image on the last page:

this.nettPriceListDocument.NewPage();
this.currentPage++;
Image logo = Image.GetInstance("/inetpub/Applications/Trade/Reps/images/TermsAndConditions.gif");
logo.SetAbsolutePosition(0, 0);
logo.ScaleToFit(this.nettPriceListDocument.PageSize.Width, this.nettPriceListDocument.PageSize.Height);
this.nettPriceListDocument.Add(logo);

I have this image as a PDF document, and I would prefer to add it. If I can understand this, it will be possible to add other PDF documents that may be required for what I create.

I tried:

string tcfile = "/inetpub/Applications/Trade/Reps/images/TermsAndConditions.pdf";
PdfReader reader = new PdfReader(tcfile);
PdfWriter writer = PdfWriter.GetInstance(this.nettPriceListDocument, this.nettPriceListMemoryStream);
PdfContentByte content = writer.DirectContentUnder;
for (int pageno = 1; pageno < reader.NumberOfPages + 1; pageno++)
{
    this.nettPriceListDocument.NewPage();
    this.currentPage++;
    PdfImportedPage newpage = writer.GetImportedPage(reader, pageno);
    content.AddTemplate(newpage, 1f, 1f);
}

Which results in a "do not open document" exception in writer.DirectContentUnder

I also tried:

string tcfile = "/inetpub/Applications/Trade/Reps/images/TermsAndConditions.pdf";
PdfReader reader = new PdfReader(tcfile);
PdfConcatenate concat = new PdfConcatenate(this.nettPriceListMemoryStream);
concat.AddPages(reader);

This results in an embedding of an odd-sized blank page over the usual first page of a document.

I also tried:

string tcfile = "/inetpub/Applications/Trade/Reps/images/TermsAndConditions.pdf";
PdfReader reader = new PdfReader(tcfile);
PdfCopy copier = new PdfCopy(nettPriceListDocument, nettPriceListMemoryStream);
for (int pageno = 1; pageno < reader.NumberOfPages + 1; pageno++)
{
    this.currentPage++;
    PdfImportedPage newpage = copier.GetImportedPage(reader, pageno);
    copier.AddPage(newpage);
}
copier.Close();
reader.Close();

NullReferenceException copier.AddPage(newpage).

:

string tcfile = "/inetpub/Applications/Trade/Reps/images/TermsAndConditions.pdf";
PdfReader reader = new PdfReader(tcfile);
PdfCopyFields copier = new PdfCopyFields(nettPriceListMemoryStream);
copier.AddDocument(reader);

NullReferenceException copier.AddDocument(reader).

StackOverflow. , , PDF , PDF . . " " , ( ), PDF- .

, : PDF , PDF?

. , , .

+5
1

, PDF PDF . PDF . , . , .

public MemoryStream MergePdfForms(List<byte[]> files)
{
    if (files.Count > 1)
    {
        PdfReader pdfFile;
        Document doc;
        PdfWriter pCopy;
        MemoryStream msOutput = new MemoryStream();

        pdfFile = new PdfReader(files[0]);

        doc = new Document();
        pCopy = new PdfSmartCopy(doc, msOutput);

        doc.Open();

        for (int k = 0; k < files.Count; k++)
        {
            pdfFile = new PdfReader(files[k]);
            for (int i = 1; i < pdfFile.NumberOfPages + 1; i++)
            {
                ((PdfSmartCopy)pCopy).AddPage(pCopy.GetImportedPage(pdfFile, i));
            }
            pCopy.FreeReader(pdfFile);
        }

        pdfFile.Close();
        pCopy.Close();
        doc.Close();

        return msOutput;
    }
    else if (files.Count == 1)
    {
        return new MemoryStream(files[0]);
    }

    return null;
}
+7

All Articles