Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3454
  • Last Modified:

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

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
sbornstein2
Asked:
sbornstein2
  • 6
  • 3
  • 3
2 Solutions
 
käµfm³d 👽Commented:
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
 
Joe Winograd, Fellow&MVEDeveloperCommented:
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
 
sbornstein2Author Commented:
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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
sbornstein2Author Commented:
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
 
sbornstein2Author Commented:
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
 
käµfm³d 👽Commented:
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
 
Joe Winograd, Fellow&MVEDeveloperCommented:
> 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
 
sbornstein2Author Commented:
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
 
sbornstein2Author Commented:
thanks for the info
0
 
Joe Winograd, Fellow&MVEDeveloperCommented:
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
 
käµfm³d 👽Commented:
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
 
sbornstein2Author Commented:
Good point Kaufmed tx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now