As you might have guessed from the title, this post is of the “get it done” variety. So, the code may be a little rough. Its the sort of code that works, but isn’t fully understood (by me at least). I wasn’t able to find a clear example on the net, so I pieced together examples and guesses based on reading the code and interpreting method and class names.
Why guess? Well, iTextSharp is a pretty robust open source project which is useful for creating and manipulating PDF documents. The problem is, it doesn’t seem to have any useful documentation online. It is open source, so you can read the code all day. Also, there are some tutorials that you can download from the files page (in the examples folder).
PdfReader reader = new PdfReader(inputStreamOrFile);
int pageCount = reader.NumberOfPages;
Rectangle pageSize = reader.GetPageSize(1);
// Setup writer
PdfDocument document = new PdfDocument(pageSize);
PdfWriter writer = PdfWriter.GetInstance(document, outputStreamOrFile);
// Copy each page
PdfContentByte content = writer.DirectContent;
for (int i = 0; i < pageCount; i++)
PdfImportedPage page = writer.GetImportedPage(reader, i + 1); // page numbers are one-based
content.AddTemplate(page, 0, 0); // x and y correspond to position on the page?
string jsText = "var res = app.setTimeOut('var pp = this.getPrintParams();pp.interactive = pp.constants.interactionLevel.full;this.print(pp);', 200);";
//string jsTextNoWait = "var pp = this.getPrintParams();pp.interactive = pp.constants.interactionLevel.full;this.print(pp);";