0

I have a web application using iText v2.1.7 to create PDFs; before anyone tries to move me to a different library, let me point out that, like most programmers, I don't choose the libraries my company uses for things, or I certainly would not use this one.

I have code that generates these PDFs; now I am to add code that takes the contents of an existing PDF and inserts it into the PDF I'm creating.

I've found examples of how to do this, but they all use files. Except for the one I'm reading, I don't have files; I'm in a web application where I don't have easy access to a place to write a file.

Can't I open the existing PDF and somehow insert its entire content into the document I'm creating, without having to write to a file?

After I do this, I will have more content to add to the document, either from another file, dynamically created content, or both, so it isn't a simple merge of my content with one existing file. I also haven't created the existing file as its own entity, to be merged with another file, though I suppose I can do that IF it's necessary.

But I was hoping there was a way (or were ways) to do this without having to reorganize my existing code. It's possible the answer is implied in one of these examples, but they don't explain the concepts behind things, so I don't know where I can put input Streams instead of file input streams, output streams instead of file output streams, etc.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • If you're not working with files, then don't let your `PdfWriter` write to a `FileOutputStream` but to a `ByteArrayOutputStream`. If you want to learn more about using iText, you can find plenty of examples on http://developers.itextpdf.com. The examples are all written by the original developer of iText. Keep in mind that the examples are written for the current version of iText (`5.x.x`). Your version, `2.1.7`, is 7 years old and is unsupported. If you need help in convincing "The Powers That Be" in your company to upgrade iText, see http://itextpdf.com/2-vs-5 – Amedee Van Gasse Jan 15 '16 at 19:28
  • Thanks; I actually came up with code to ask about, it's at http://stackoverflow.com/questions/34818288/itext-2-1-7-pdfcopy-addpagepage-cant-find-page-reference - it has a ByteArrayOutputStream which is the one being written to for the document -- is that the right one to use? – arcy Jan 15 '16 at 19:40
  • Well, I've seen other people use `ByteArrayOutputStream`. `PdfWriter.getInstance` wants an `OutputStream`, as you can see in the API documentation: http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfWriter.html. `ByteArrayOutputStream` is one of the subclasses of `OutputStream`, so yes that sounds like valid code to me - unrelated to the error in your other question. – Amedee Van Gasse Jan 15 '16 at 20:30
  • Are this question and that other question about the same thing? Help yourself, put all your relevant information in one question, and close the other one. You can always edit your question when you have more details. – Amedee Van Gasse Jan 15 '16 at 20:32
  • yes, the two posts are about the same thing; I can delete this one, but would appreciate it if you'd look at the other one. – arcy Jan 15 '16 at 20:50
  • @arcy You'll notice that there is little enthusiasm to answer your questions. Allow me to explain why: any answer to a question about iText 2.1.7 amounts to a sticking plaster on a wooden leg. A self-respecting developer uses the latest versions of iText. We pity developers who choose to use a version that dates from 2009 (or who accepts orders to do so), but that pity doesn't go so far that we are willing to spend our precious time answering their questions. Please upgrade (or ask your employer to consider upgrading) to iText 5.5.8. – Bruno Lowagie Jan 16 '16 at 12:05

0 Answers0