I've been browsing the net for almost 3 hrs now without a clue. Found many different examples but nothing that has lead me in the right direction.
I have a PDF template file that I open and fill from code behind. This works just fine. Now I have an additional thing I need to do. I need to insert an image into the PDF file at a specific point. Doing absolute positioning is not a problem. The problem
is that I can't find how to insert the image without it having to be a new document. Can I use any of the below code to add it?
PdfReader
reader = new
PdfReader(fileNameFrom);
PdfStamper stamper =
new PdfStamper(reader,
new FileStream(fileNameTo,
FileMode.Create));
AcroFields fields = stamper.AcroFields;
iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(Server.MapPath("/img/logo1.jpg"));
PdfPCell cell = new PdfPCell(logo);
I use this and it works for sure . Do you mean that you get no errors when you create the document and the problem occurs when you have to write on a completed doc? what type of error do you get?
iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(Server.MapPath("/img/logo1.jpg"));
PdfPCell cell = new PdfPCell(logo);
I use this and it works for sure . Do you mean that you get no errors when you create the document and the problem occurs when you have to write on a completed doc? what type of error do you get?
That would work if I was creating a table into a new document. I haven't a table as this isn't a new document but a template that I use. I need to insert the image into the document that is created from the template. I was playing with:
but I couldn't get it to work either. Kept on giving me the error "object not set to a reference" on the addImage. This is the same error message I would get before.
All the examples I found were creating a new document but none from a template.
I was able to figure it out. For those who are interested you do:
stamper.GetInstance(pageNumber).AddImage(image);
This will add an image on the page you specified. If you do set the absolute position of the image you specify where you want it on the page. It starts the coordinates of the page from the bottom left.
Would you be able to post your code?
I'm working on using iTextSharp to insert data from code behind into a PDF template that has already been created but I'm not 100% sure how to do it.
I am interested in the solution, could you please post your code guys?
I am stuck on this one, I can't find a good documentation (stamper, not stamper...)
I get
Error 1 'iTextSharp.text.pdf.PdfStamper' does not contain a definition for 'GetInstance' and no extension method 'GetInstance' accepting a first argument of type 'iTextSharp.text.pdf.PdfStamper' could be found (are you missing a using directive or
an assembly reference?)
when I try
PdfReader reader = new PdfReader("PublishedCertificate.pdf");
PdfStamper stamp = new PdfStamper(reader, new FileStream("withchart.pdf", FileMode.Create));
stamp.GetInstance(1).AddImage("zedgraph.png");
stamp.Close();
I've tried several other techniques but none of them work !!
Hey Guys I am going to give more details about the issue:
I am trying to add a chart from a png image file which I know exists and put it in an existing PDF, all in the same folder. I manage to create a PDF from a template and add some text on top of it before I try to add the image in.
My code:
// we create a reader for a certain document, Certificate.pdf serves as the template
PdfReader readerCertificate = new PdfReader("Certificate.pdf");
// we retrieve the size of the first page
iTextSharp.text.Rectangle psize = readerCertificate.GetPageSize(1);
float width = psize.Width;
float height = psize.Height;
// step 1: creation of a document-object
Document document = new Document(psize, 50, 50, 50, 50);
// step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("PublishedCertificate.pdf", FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we add content=I add the template in the New document
PdfContentByte cb = writer.DirectContent;
document.NewPage();
PdfImportedPage page1 = writer.GetImportedPage(readerCertificate, 1);
cb.AddTemplate(page1, 0,0);
//add content above the template= I add some text on the new PDF
iTextSharp.text.pdf.BaseFont bf = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.COURIER, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED); cb.BeginText();
cb.SetFontAndSize(bf, 12);
AddTextInPDF(cb, 5.1, 8, SpecMaxRsd.ToString());
AddTextInPDF(cb, 6, 8.6, SpecMaxDev.ToString());
cb.EndText();
document.Close();
//add chart=here comes the troubles
PdfReader reader = new PdfReader("PublishedCertificate.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileStream("withchart.pdf", FileMode.Create));
AcroFields fields = stamper.AcroFields;
//set images
string chartLoc = string.Empty;
iTextSharp.text.Image chartImg = iTextSharp.text.Image.GetInstance("zedgraph.png");
chartImg.SetAbsolutePosition(50, 50);
PdfContentByte cB = new PdfContentByte(stamper.Writer);
cB.AddImage(chartImg);
It all goes well until line31 where I get this error in the Output of Visual Studio:
'GenericTest.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.resources\2.0.0.0_fr_b77a5c561934e089\System.resources.dll'
'GenericTest.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
A first chance exception of type 'System.Net.WebException' occurred in System.dll
The thread 0x9b0 has exited with code 0 (0x0).
It looks like I can't access the image file properly for some reason. I don't understand why I am seeing a WebException since I have nothing Web in this code...
bobothebugbe...
Member
628 Points
229 Posts
iTextSharp: inserting an image?
Mar 31, 2008 09:17 PM|LINK
I've been browsing the net for almost 3 hrs now without a clue. Found many different examples but nothing that has lead me in the right direction.
I have a PDF template file that I open and fill from code behind. This works just fine. Now I have an additional thing I need to do. I need to insert an image into the PDF file at a specific point. Doing absolute positioning is not a problem. The problem is that I can't find how to insert the image without it having to be a new document. Can I use any of the below code to add it?
PdfReader
reader = new PdfReader(fileNameFrom);PdfStamper stamper = new PdfStamper(reader, new FileStream(fileNameTo, FileMode.Create));
AcroFields fields = stamper.AcroFields;
string
chartLoc = string.Empty;chartLoc = "reports/report1.png";
iTextSharp.text.Image chartImg = iTextSharp.text.Image.GetInstance(chartLoc);
chartImg.SetAbsolutePosition(50, 500);
The images are all created before the PDF is so they will be referenced by name.
Any help would be greatly appreciated.
thanks ^_^
joppo
Member
624 Points
153 Posts
Re: iTextSharp: inserting an image?
Apr 01, 2008 06:37 AM|LINK
iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(Server.MapPath("/img/logo1.jpg"));
PdfPCell cell = new PdfPCell(logo);
I use this and it works for sure . Do you mean that you get no errors when you create the document and the problem occurs when you have to write on a completed doc? what type of error do you get?
bobothebugbe...
Member
628 Points
229 Posts
Re: iTextSharp: inserting an image?
Apr 02, 2008 04:06 PM|LINK
That would work if I was creating a table into a new document. I haven't a table as this isn't a new document but a template that I use. I need to insert the image into the document that is created from the template. I was playing with:
//set images
string chartLoc = string.Empty;chartLoc = Server.MapPath("reports/" + chartNames[0].ToString() + ".png");
iTextSharp.text.Image chartImg = iTextSharp.text.Image.GetInstance(chartLoc);
chartImg.SetAbsolutePosition(50, 50); PdfContentByte cB = new PdfContentByte(stamper.Writer);
cB.AddImage(chartImg);
but I couldn't get it to work either. Kept on giving me the error "object not set to a reference" on the addImage. This is the same error message I would get before.
All the examples I found were creating a new document but none from a template.
thanks ^_^
bobothebugbe...
Member
628 Points
229 Posts
Re: iTextSharp: inserting an image?
Apr 14, 2008 04:41 PM|LINK
I was able to figure it out. For those who are interested you do:
stamper.GetInstance(pageNumber).AddImage(image);
This will add an image on the page you specified. If you do set the absolute position of the image you specify where you want it on the page. It starts the coordinates of the page from the bottom left.
^_^
schuminator
Member
498 Points
214 Posts
Re: iTextSharp: inserting an image?
Apr 15, 2008 12:15 AM|LINK
Would you be able to post your code?
I'm working on using iTextSharp to insert data from code behind into a PDF template that has already been created but I'm not 100% sure how to do it.
Thanks
schuminator
Member
498 Points
214 Posts
Re: iTextSharp: inserting an image?
Apr 15, 2008 02:13 PM|LINK
Ahh never mind, I figured it out ;)
popopol
Member
8 Points
4 Posts
Re: iTextSharp: inserting an image?
May 07, 2008 01:31 PM|LINK
Hi there
I am interested in the solution, could you please post your code guys?
I am stuck on this one, I can't find a good documentation (stamper, not stamper...)
I get
Error 1 'iTextSharp.text.pdf.PdfStamper' does not contain a definition for 'GetInstance' and no extension method 'GetInstance' accepting a first argument of type 'iTextSharp.text.pdf.PdfStamper' could be found (are you missing a using directive or an assembly reference?)
when I try
PdfReader reader = new PdfReader("PublishedCertificate.pdf"); PdfStamper stamp = new PdfStamper(reader, new FileStream("withchart.pdf", FileMode.Create)); stamp.GetInstance(1).AddImage("zedgraph.png"); stamp.Close();I've tried several other techniques but none of them work !!
Please help
Thank you so much in advance[:D]
popopol
Member
8 Points
4 Posts
Re: iTextSharp: inserting an image?
May 07, 2008 01:52 PM|LINK
Hey Guys I am going to give more details about the issue:
I am trying to add a chart from a png image file which I know exists and put it in an existing PDF, all in the same folder. I manage to create a PDF from a template and add some text on top of it before I try to add the image in.
My code:
// we create a reader for a certain document, Certificate.pdf serves as the template PdfReader readerCertificate = new PdfReader("Certificate.pdf"); // we retrieve the size of the first page iTextSharp.text.Rectangle psize = readerCertificate.GetPageSize(1); float width = psize.Width; float height = psize.Height; // step 1: creation of a document-object Document document = new Document(psize, 50, 50, 50, 50); // step 2: we create a writer that listens to the document PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("PublishedCertificate.pdf", FileMode.Create)); // step 3: we open the document document.Open(); // step 4: we add content=I add the template in the New document PdfContentByte cb = writer.DirectContent; document.NewPage(); PdfImportedPage page1 = writer.GetImportedPage(readerCertificate, 1); cb.AddTemplate(page1, 0,0); //add content above the template= I add some text on the new PDF iTextSharp.text.pdf.BaseFont bf = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.COURIER, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED); cb.BeginText(); cb.SetFontAndSize(bf, 12); AddTextInPDF(cb, 5.1, 8, SpecMaxRsd.ToString()); AddTextInPDF(cb, 6, 8.6, SpecMaxDev.ToString()); cb.EndText(); document.Close(); //add chart=here comes the troubles PdfReader reader = new PdfReader("PublishedCertificate.pdf"); PdfStamper stamper = new PdfStamper(reader, new FileStream("withchart.pdf", FileMode.Create)); AcroFields fields = stamper.AcroFields; //set images string chartLoc = string.Empty; iTextSharp.text.Image chartImg = iTextSharp.text.Image.GetInstance("zedgraph.png"); chartImg.SetAbsolutePosition(50, 50); PdfContentByte cB = new PdfContentByte(stamper.Writer); cB.AddImage(chartImg);It all goes well until line31 where I get this error in the Output of Visual Studio:'GenericTest.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.resources\2.0.0.0_fr_b77a5c561934e089\System.resources.dll'
'GenericTest.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
A first chance exception of type 'System.Net.WebException' occurred in System.dll
The thread 0x9b0 has exited with code 0 (0x0).
It looks like I can't access the image file properly for some reason. I don't understand why I am seeing a WebException since I have nothing Web in this code...
Please help :))
Thanks again
popopol
Member
8 Points
4 Posts
Re: iTextSharp: inserting an image?
May 07, 2008 01:59 PM|LINK
Last post hopefuly, apologies for writing several times...
I forgot to mention, I do not get an error as such, it's just messages in the output of VS, and the output PDF withchart.pdf is created but corrupted.
Content of the PDF file if of any help:
%PDF-1.4
%âãÏÓ
rambha_yj
Member
2 Points
1 Post
Re: iTextSharp: inserting an image?
May 11, 2008 01:50 AM|LINK
try this code, it should work perfectly. It adds an image at a given position without fail on any page of the pdf file. I tested many times
string
pdfTemplate = @"C:\PDFFileSoftware\PdfGenerator_CS\PdfGenerator_CS\PdfGenerator\1_Form SS-4 (2007).pdf"; //@"c:\Temp\PDF\fw4.pdf"; string newFile = @"c:\Temp\PDF\completed_fw4.pdf"; PdfReader pdfReader = new PdfReader(pdfTemplate); PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile,
FileMode.Create)); AcroFields pdfFormFields = pdfStamper.AcroFields; string chartLoc = string.Empty;chartLoc =
@"C:\Temp\PDF\IMG_3746.jpg";//pplLogoSmall.jpg"; iTextSharp.text.Image chartImg = iTextSharp.text.Image.GetInstance(chartLoc);iTextSharp.text.pdf.
PdfContentByte underContent; iTextSharp.text.Rectangle rect; try{
Single X, Y;int pageCount = 0;rect = pdfReader.GetPageSizeWithRotation(1);
if (chartImg.Width > rect.Width || chartImg.Height > rect.Height){
chartImg.ScaleToFit(rect.Width, rect.Height);
X = (rect.Width - chartImg.ScaledWidth) / 2;
Y = (rect.Height - chartImg.ScaledHeight) / 2;
}
else{
X = (rect.Width - chartImg.Width) / 2;
Y = (rect.Height - chartImg.Height) / 2;
} chartImg.SetAbsolutePosition(X, Y);
pageCount = pdfReader.NumberOfPages;
for (int i = 1; i < pageCount; i++){
underContent = pdfStamper.GetOverContent(i);
//.GetUnderContent(i);underContent.AddImage(chartImg);
}
pdfStamper.Close();
pdfReader.Close();
}
catch (Exception ex){
throw ex;}