Solved

iTextSharp - create multi-page PDF batch with merge or something else?

Posted on 2013-11-08
12
2,770 Views
Last Modified: 2013-11-08
Hello all,

This is my scenario.   I have to create an Invoice Batch file.  Users will select multiple orders from a grid then I need to loop through these orders and create an invoice for each order.   Here is the caveat.   For each order there is another table called order_docs that has an order number and physical file location on the network to a scanned PDF document.   For each order I create an invoice for I need to attach 'behind' that invoice the scanned PDF document if it exists then continue for each order, invoice then behind it scanned pdf merged into the next pages for example.

I assume I may need to use a console application to handle this scenario, launch from MVC app or put in queue table then console app would create the file.  Worst case scenario I will be sending it direct to the printer within a loop.

Any suggestions on this, best tool to use aka can iTextSharp handle this SSRS etc?

Hope this makes sense looking for best possible approach.
0
Comment
Question by:sbornstein2
  • 6
  • 3
  • 3
12 Comments
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
Comment Utility
I am currently using PDFSharp to combine single PDFs into one PDF file. When I was proofing  this, it only took a few lines of code:

using MoreLinq;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;

namespace PdfSharpTest
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument pdf1 = PdfReader.Open(@"first.pdf", PdfDocumentOpenMode.Import);
            PdfDocument pdf2 = PdfReader.Open(@"second.pdf", PdfDocumentOpenMode.Import);
            PdfDocument pdf3 = new PdfDocument();

            pdf1.Pages.PagesArray.ForEach(page => pdf3.Pages.Add((PdfPage)page));
            pdf2.Pages.PagesArray.ForEach(page => pdf3.Pages.Add((PdfPage)page));

            pdf3.Save(@"result.pdf");
        }
    }
}

Open in new window


PDFSharp is available from NuGet.

You don't necessarily need MoreLINQ. If you don't install that as well, then you simply need to rework the two ForEach lines into actual foreach statements.
0
 
LVL 51

Accepted Solution

by:
Joe Winograd, EE MVE earned 250 total points
Comment Utility
I've used the PDF Toolkit (PDFtk) to perform similar operations. It is an excellent (free!) product that has numerous features to manipulate PDFs. It comes in both command line and GUI versions. The command line version is called PDFtk Server and may be downloaded here:
http://www.pdflabs.com/tools/pdftk-server/

Don't be misled by "Server" in the name. I don't know why they called it that, but it's just an executable (pdftk.exe – with a supporting DLL, libiconv2.dll) that runs on XP, Vista, W7, and W8 (it does not have to run on a "server" OS...it also runs on Mac, but I've never used it on that).

Here's the manual (Man Page) for it:
http://www.pdflabs.com/docs/pdftk-man-page/

To do what you want, take a look at the "cat" parameter, which combines ("catentates") page ranges from multiple PDFs, and the "shuffle" parameter, which works like "cat" but processes one page at a time.

Here's an example of a program that uses the "shuffle" parameter, looping through all pages in multiple files:
http://www.experts-exchange.com/Software/Misc/A_11211-How-To-Split-Rename-Move-a-Batch-of-PDF-Files-Based-on-Contents-of-the-Files.html

It's a long article and you may not want to read the whole thing, but take a look at the code snippets that build up the "shuffle" parameter and then call <pdftk.exe> with it (via %comspec%).

I've had excellent results with this free tool. Regards, Joe
0
 

Author Comment

by:sbornstein2
Comment Utility
awesome info.   So the invoices I need to create reports for could be many for example say 50 invoices I need to create a report page for aka PDF then use the merge as well.   Can both tools create such dynamic reports as well?   I thought iTextSharp take for example an HTML page and then creates a PDF for example.  So in my case I would have many of these.   Essentially I have a recordset of data from a SQL query and each record would need to become a PDF page or page(s).
0
 

Author Comment

by:sbornstein2
Comment Utility
I will reward both Kaufmed and Joewin.   Thanks again guys I need to figure out what I should start experimenting this scenario with this weekend.
0
 

Author Comment

by:sbornstein2
Comment Utility
Creating a batch file per say is what at the end of the day I am wanting.  New PDF pages as well as merging some existing PDF files with it.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
I have to confess that I do not know all of the functionality available from PDFSharp--I've only started using it recently. In glancing at the source code, it appears that the library has functionality embedded for drawing PDFs yourself, so it may just be a matter or parsing the source HTML for data and outputting it to a new PDF canvas. I cannot confirm this, though.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
Comment Utility
> Can both tools create such dynamic reports as well?

I can't speak to PDFsharp...never used it. Re PDFtk, it cannot create PDF files from scratch. Its purpose is to manipulate existing PDFs, so it can merge/combine PDFs for you, but not create them (except from existing PDFs).

> take for example an HTML page and then creates a PDF

You could try a PDF print driver with command line capability. One such product is Bullzip:
http://www.bullzip.com/products/pdf/info.php

It is free and provides a command line interface to all settings, as well as a COM/ActiveX interface that you can use in programs. This free version (which is very good!) is based on the commercial version of the bioPDF PDF Writer:
http://www.biopdf.com/

There are some features in the commercial version that aren't in the free version, but I don't think you'll need them (although I'm not sure of that).

There are many free PDF print drivers out there...Bullzip, CutePDF Writer, doPDF, Nitro PDF Creator (part of the Nitro Reader install), PDFCreator, PrimoPDF, to name a few...although not many of them provide a command line or programmatic interface.

For HTML files specifically, A-PDF HTML to PDF is a non-free, but relatively inexpensive ($39) product that has a command line interface (Htmltopdf.exe).

> recordset of data from a SQL query and each record would need to become a PDF page or page(s).

Should be fine if you can "print" the results of the SQL query to the Bullzip print driver (or whatever PDF print driver you wind up using).

> I should start experimenting this scenario with this weekend.

No rush for the points here. Take the weekend or as long as you need. Looking forward to hearing how it goes for you.

> Creating a batch file per say is what at the end of the day I am wanting. New PDF pages as well as merging some existing PDF files with it.

Should be easy to create a batch file, as long as you can find the necessary command line components.

Regards, Joe
0
 

Author Comment

by:sbornstein2
Comment Utility
Last opinion I promise then I will award 250 to each of you guys thanks so much again.  So I have been playing around today and iTextSharp I can do this for sure.  I am testing now creating a Document page then doing a PdfReader where I point to a file on disk on my local C drive share UNC path.  I loop through the pages doing a Document.NewPage() and this actually works so that is very cool.   I did this for two existing PDF documents in between created document pages I am messing with.

My worry is though doing this through an MVC application and response to the browser this PDF document and opening PDFReaders for like 50 multi-page docs may not be feasible with memory or the write flush not being able to handle it.

Any thoughts?   I am thinking should I use a console application external to the MVC application, queue it up for example then let the console app do the work.
0
 

Author Closing Comment

by:sbornstein2
Comment Utility
thanks for the info
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
Comment Utility
I haven't built any MVC apps, so I can't help you with that. Your reasoning makes sense to me on a general basis, but the specifics of an MVC app is beyond my areas of expertise. Thanks for awarding the points – much appreciated! Good luck on the project. Regards, Joe
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
I am thinking should I use a console application external to the MVC application, queue it up for example then let the console app do the work.
If you're running this console application on the same server, then you're eating memory regardless of which method you choose.
0
 

Author Comment

by:sbornstein2
Comment Utility
Good point Kaufmed tx
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now