How to merge multiple pdf files (generated on run time) through ItextSharp
then printing them.
I found the following link but that method requires the pdf names considering that the pdf files stored and this is not my case .
I have multiple reports i'll convert them to pdf files
through this method :
private void AddReportToResponse(LocalReport followsReport)
{
string mimeType;
string encoding;
string extension;
string[] streams = new string[100];
Warning[] warnings = new Warning[100];
byte[] pdfStream = followsReport.Render("PDF", "", out mimeType, out encoding, out extension, out streams, out warnings);
//Response.Clear();
//Response.ContentType = mimeType;
//Response.AddHeader("content-disposition", "attachment; filename=Application." + extension);
//Response.BinaryWrite(pdfStream);
//Response.End();
}
Now i want to merge all those generated files (Bytes
) in one pdf file to print it
I used iTextsharp with c# to combine pdf files. This is the code I used.
Here is some code I pulled out of an old project I had. It was a web application but I was using iTextSharp to merge pdf files then print them.
I didn't write this, but made some modifications. I can't remember where I found it. After I merged the PDFs I would call this method to insert javascript to open the print dialog when the PDF is opened. If you change bSilent to true then it should print silently to their default printer.
Not sure how well the above code is written since I pulled it from somewhere else and I haven't worked in depth at all with iTextSharp but I do know that it did work at merging PDFs that I was generating at runtime.
To avoid the memory issues mentioned, I used file stream instead of memory stream(mentioned in ITextSharp Out of memory exception merging multiple pdf) to merge pdf files:
Tested with iTextSharp-LGPL 4.1.6:
If you want to merge source documents using iText(Sharp), there are two basic situations:
You really want to merge the documents, acquiring the pages in their original format, transfering as much of their content and their interactive annotations as possible. In this case you should use a solution based on a member of the
Pdf*Copy*
family of classes.You actually want to integrate pages from the source documents into a new document but want the new document to govern the general format and don't care for the interactive features (annotations...) in the original documents (or even want to get rid of them). In this case you should use a solution based on the
PdfWriter
class.You can find details in chapter 6 (especially section 6.4) of iText in Action — 2nd Edition. The Java sample code can be accessed here and the C#'ified versions here.
A simple sample using
PdfCopy
is Concatenate.java / Concatenate.cs. The central piece of code is:Here
pdf
can either be defined as aList<byte[]>
immediately containing the source documents (appropriate for your use case of merging intermediate in-memory documents) or as aList<String>
containing the names of source document files (appropriate if you merge documents from disk).An overview at the end of the referenced chapter summarizes the usage of the classes mentioned:
PdfCopy
: Copies pages from one or more existing PDF documents. Major downsides:PdfCopy
doesn’t detect redundant content, and it fails when concatenating forms.PdfCopyFields
: Puts the fields of the different forms into one form. Can be used to avoid the problems encountered with form fields when concatenating forms usingPdfCopy
. Memory use can be an issue.PdfSmartCopy
: Copies pages from one or more existing PDF documents.PdfSmartCopy
is able to detect redundant content, but it needs more memory and CPU thanPdfCopy
.PdfWriter
: Generates PDF documents from scratch. Can import pages from other PDF documents. The major downside is that all interactive features of the imported page (annotations, bookmarks, fields, and so forth) are lost in the process.