Hey,
I'm working on a project that uses iTextSharp to output a PDF. As part of the
project, I'd like to be able to import existing PDFs and turn the pages into
images. I can then manipulate these pages as Image objects (e.g. resize them,
rotate, etc.) and then add them to the output PDF.
>From what I can see, there are two ways to use iTextSharp to import a PDF and
>turn a page into an Image object:
1. using the GetImportedPage method from the PdfWriter class
2. using the GetImportedPage method from the PdfStamper class
I'm having trouble getting either of these two methods to work the way I want
them to.
Firstly I tried to create a very simple program which reads in a PDF, turns it
into an Image, and writes it to a new PDF
-------------
static void Main(string[] args)
{
// Create the document I want to write. Create a writer for it and
then open it.
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, new
FileStream("output.pdf", FileMode.Create));
document.Open();
// Use the writer to read an input .pdf into a PdfImportedPage and
then turn into an image
PdfImportedPage page = writer.GetImportedPage(new
PdfReader("input.pdf"), 1);
Image image = Image.GetInstance(page);
// Add image to the document I want to write, and then close it.
document.Add(image);
document.Close();
}
-------------
This worked completely as expected.
But what I actually need is some stand alone method that can read in input.pdf
and return a Image object, which I can then manipulate and add to output.pdf
with other methods.
So I tried this:
-------------
static void Main(string[] args)
{
// Create the document I want to write. Create a writer for it and
then open it.
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, new
FileStream("output.pdf", FileMode.Create));
document.Open();
// Call a method, possibly in another class, to import a .PDF and
turn it into a picture
Image image = method1("input.pdf");
// Call a method to write the image to the document
document.Add(image);
document.Close();
}
static private Image method1(string input)
{
PdfWriter tmpWriter = PdfWriter.GetInstance(new Document(), new
MemoryStream());
PdfImportedPage page = tmpWriter.GetImportedPage(new
PdfReader(input), 1);
Image image = Image.GetInstance(page);
return image;
}
-------------
When I run this, the program stops at the line "document.Add(image)" with the
error "Object reference not set to an instance of an object". What am I doing
wrong here? Why is the image returned by method1 not valid? It seems the Image
object method1 produces is somehow still tied with tmpWriter, and will only
work in that method.
Changing the code to:
-------------
static void Main(string[] args)
{
// Create the document I want to write. Create a writer for it and
then open it.
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, new
FileStream("output.pdf", FileMode.Create));
document.Open();
// Call a method, possibly in another class, to import a .PDF and
turn it into a picture
Image image = method1("input.pdf", writer);
// Call a method to write the image to the document
document.Add(image);
document.Close();
}
static private Image method1(string input, PdfWriter writer)
{
PdfImportedPage page = writer.GetImportedPage(new PdfReader(input),
1);
Image image = Image.GetInstance(page);
return image;
}
-------------
Words and the PDF produced is as expected. But for various reasons, it's not
very feasible in my project to pass the writer of the document I am ultimately
writing to, to the method reading in the PDF. I really need a way to create a
method that can just read in a PDF when given the name of a PDF, and return an
Image that can be stored (e.g. in a ArrayList<Image> of Images), and then
fished out elsewhere in the program, and then manipulated and added to an
output PDF.
I also tried using PdfStamper instead, but a simple test program:
-------------
static void Main(string[] args)
{
// Create the document I want to write. Create a writer for it and
then open it.
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, new
FileStream("output.pdf", FileMode.Create));
document.Open();
// Use the writer to read an input .pdf into a PdfImportedPage and
then turn into an image
PdfReader reader = new PdfReader("input.pdf");
PdfStamper stamper = new PdfStamper(reader, new MemoryStream());
PdfImportedPage p = stamper.GetImportedPage(reader, 1);
Image image = Image.GetInstance(p);
// Add image to the document I want to write, and then close it.
document.Add(image);
document.Close();
}
-------------
Produces a 1kb output.pdf that does not actually open. What is wrong with my
use of PdfStamper here?
Would anyone know how to use PdfWriter or PdfStamper's GetImportedPage methods
to do what I need to do? Or does anyone know any alternative methods to read in
an input PDF and turn it into an Image object that I can pass onto other
methods/classes freely?
Thanks
_________________________________________________________________
Get the latest news, goss and sport Make ninemsn your homepage!
http://windowslive.ninemsn.com.au/article.aspx?id=813730------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions:
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/