On 22/08/2013 21:44, Vahid Nasiri wrote:
Hello,
The following sample does not work with the latest iTextSharp.

There are at least two errors in your code, so I didn't read on.

Error 1 (not fatal): you're creating a new Image instance every time OnStartPage() is triggered. This means that you add the same bytes over and over to your document, leading to a bloated file.

Error 2 (unforgivable): as documented, the Document object passed to the OnStartPage() method is NOT the same object as the Document object used in the rest of your code. Instead, it's an internal PdfDocument instance that should be used for read-only purposes only. In other words: pdfDoc.Add(table); is forbidden. It shouldn't work.

I didn't read on after this second error. You code needs to be rewritten before one can comment any further on this topic.

The produced PDF file has not the related XObjects of the image file. So the Adobe reader is not able to show the image.
But this code works fine with iTextSharp 5.4.1.
I found a simple solution for that: I have to move the lines after the `pdfDoc.Open` before it, to make it work.
using System.Diagnostics;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace OptimizeImageSizes
{
public class PageEvents : PdfPageEventHelper
{
public override void OnStartPage(PdfWriter writer, Document pdfDoc)
{
base.OnStartPage(writer, pdfDoc);
var table = new PdfPTable(1);
var img = Image.GetInstance("myImage.png");
var cell = new PdfPCell(img, false);
table.AddCell(cell);
pdfDoc.Add(table);
}
}
public static class CrashWith534
{
public static void Test()
{
using (var pdfDoc = new Document(PageSize.A4))
{
var pdfWriter = PdfWriter.GetInstance(pdfDoc, new FileStream("Test.pdf", FileMode.Create));
pdfWriter.PageEvent = new PageEvents();
pdfDoc.Open();
pdfWriter.SetPdfVersion(new PdfName("1.5"));
pdfWriter.SetFullCompression();
}
Process.Start("Test.pdf");
}
}
}


------------------------------------------------------------------------------
Introducing Performance Central, a new site from SourceForge and
AppDynamics. Performance Central is your source for news, insights,
analysis and resources for efficient Application Performance Management.
Visit us today!
http://pubads.g.doubleclick.net/gampad/clk?id=48897511&iu=/4140/ostg.clktrk


_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

------------------------------------------------------------------------------
Introducing Performance Central, a new site from SourceForge and 
AppDynamics. Performance Central is your source for news, insights, 
analysis and resources for efficient Application Performance Management. 
Visit us today!
http://pubads.g.doubleclick.net/gampad/clk?id=48897511&iu=/4140/ostg.clktrk
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to