approach to combine multiple PDFs from templates

View: New views
2 Messages — Rating Filter:   Alert me  

approach to combine multiple PDFs from templates

by aaron.riley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm stuck on this one. Any help would be appreciated.

The plan is to have produce a multi-page PDF consisting of one or more generated PDFs. The front-end code will have checkboxes to select which PDFs to include.

The generated PDFs (FileCoverOE2(), BookingConfirmationOE2()) accept a memory stream, read from a template file with acrobat fields, insert values from database calls, flatten the file and then return the memory stream. That works without a problem in a single-use scenario.

I was hoping to merge memorystreams but it seems that it's being overwritten with the last memorystream as only 1 page is inserted (I expect 2 pages, one for each PDF). Does anyone have an approach to produce a multi-page PDF under this setup? Maybe use something other than a memorystream?

Thanks in advance!


protected void Page_Load(object sender, EventArgs e)
{
        MemoryStream stream = PdfUtils.CreatePDF2();
        SendOutPDF(stream);
}
protected void SendOutPDF(MemoryStream PdfData)
{
        // Clear response content & headers
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.ContentType = "application/pdf";
        Response.Charset = string.Empty;
        Response.Cache.SetCacheability(System.Web.HttpCacheability.Public);
        Response.AddHeader("Content-Disposition",
        "attachment; filename=" + Title.Replace(" ", "").Replace(":", "-") + ".pdf");
        Response.OutputStream.Write(PdfData.GetBuffer(), 0, PdfData.GetBuffer().Length);
        Response.OutputStream.Flush();
        Response.OutputStream.Close();
        Response.End();
}
public static MemoryStream CreatePDF2()
{
        MemoryStream PDFData = new MemoryStream();
        MemoryStream PDFData2 = new MemoryStream();
        Document document = new Document();
        PdfWriter PDFWriter = PdfWriter.GetInstance(document, PDFData);
        PDFData = FileCoverOE2(PDFData);
        PDFData2 = BookingConfirmationOE2(PDFData2);
        PDFData.Write(PDFData2.GetBuffer(), 0, (int)PDFData2.Length);
        return PDFData;
}
public static MemoryStream FileCoverOE2(MemoryStream PDFData)
{
        string template = @"C:\Inetpub\wwwroot\Templates\FileCover.pdf";
        PdfReader reader = new PdfReader(template);
        PdfStamper stamper = new PdfStamper(reader, PDFData);
        AcroFields fields = stamper.AcroFields;
        fields.SetField("Field1", "test");
...

        stamper.FormFlattening = true;
        stamper.Writer.CloseStream = false;
        stamper.Close();
        return PDFData;
}
public static MemoryStream BookingConfirmationOE2(MemoryStream PDFData)
{
        string template = @"C:\Inetpub\wwwroot\Templates\BookingConfirmation.pdf";
        PdfReader reader = new PdfReader(template);
        PdfStamper stamper = new PdfStamper(reader, PDFData);
        AcroFields fields = stamper.AcroFields;
        fields.SetField("Field1", "test");
...

        stamper.FormFlattening = true;
        stamper.Writer.CloseStream = false;
        stamper.Close();
        return PDFData;
}

Re: approach to combine multiple PDFs from templates

by Paulo Soares-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

PDFs can't be concatenated just by adding one MemoryStream to another, you
must use PdfCopy.

Paulo

----- Original Message -----
From: "aaron.riley" <aaron.riley@...>
To: <itextsharp-questions@...>
Sent: Friday, September 11, 2009 9:58 PM
Subject: [itextsharp-questions] approach to combine multiple PDFs from
templates



I'm stuck on this one. Any help would be appreciated.

The plan is to have produce a multi-page PDF consisting of one or more
generated PDFs. The front-end code will have checkboxes to select which PDFs
to include.

The generated PDFs (FileCoverOE2(), BookingConfirmationOE2()) accept a
memory stream, read from a template file with acrobat fields, insert values
from database calls, flatten the file and then return the memory stream.
That works without a problem in a single-use scenario.

I was hoping to merge memorystreams but it seems that it's being overwritten
with the last memorystream as only 1 page is inserted (I expect 2 pages, one
for each PDF). Does anyone have an approach to produce a multi-page PDF
under this setup? Maybe use something other than a memorystream?

Thanks in advance!


protected void Page_Load(object sender, EventArgs e)
{
MemoryStream stream = PdfUtils.CreatePDF2();
SendOutPDF(stream);
}
protected void SendOutPDF(MemoryStream PdfData)
{
// Clear response content & headers
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.Charset = string.Empty;
Response.Cache.SetCacheability(System.Web.HttpCacheability.Public);
Response.AddHeader("Content-Disposition",
"attachment; filename=" + Title.Replace(" ", "").Replace(":", "-") +
".pdf");
Response.OutputStream.Write(PdfData.GetBuffer(), 0,
PdfData.GetBuffer().Length);
Response.OutputStream.Flush();
Response.OutputStream.Close();
Response.End();
}
public static MemoryStream CreatePDF2()
{
MemoryStream PDFData = new MemoryStream();
MemoryStream PDFData2 = new MemoryStream();
Document document = new Document();
PdfWriter PDFWriter = PdfWriter.GetInstance(document, PDFData);
PDFData = FileCoverOE2(PDFData);
PDFData2 = BookingConfirmationOE2(PDFData2);
PDFData.Write(PDFData2.GetBuffer(), 0, (int)PDFData2.Length);
return PDFData;
}
public static MemoryStream FileCoverOE2(MemoryStream PDFData)
{
string template = @"C:\Inetpub\wwwroot\Templates\FileCover.pdf";
PdfReader reader = new PdfReader(template);
PdfStamper stamper = new PdfStamper(reader, PDFData);
AcroFields fields = stamper.AcroFields;
fields.SetField("Field1", "test");
...

stamper.FormFlattening = true;
stamper.Writer.CloseStream = false;
stamper.Close();
return PDFData;
}
public static MemoryStream BookingConfirmationOE2(MemoryStream PDFData)
{
string template = @"C:\Inetpub\wwwroot\Templates\BookingConfirmation.pdf";
PdfReader reader = new PdfReader(template);
PdfStamper stamper = new PdfStamper(reader, PDFData);
AcroFields fields = stamper.AcroFields;
fields.SetField("Field1", "test");
...

stamper.FormFlattening = true;
stamper.Writer.CloseStream = false;
stamper.Close();
return PDFData;
}
--
View this message in context:
http://www.nabble.com/approach-to-combine-multiple-PDFs-from-templates-tp25408209p25408209.html
Sent from the itextsharp-questions mailing list archive at Nabble.com.


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus
on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
itextsharp-questions mailing list
itextsharp-questions@...
https://lists.sourceforge.net/lists/listinfo/itextsharp-questions 


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
itextsharp-questions mailing list
itextsharp-questions@...
https://lists.sourceforge.net/lists/listinfo/itextsharp-questions