Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

adobe - PDFs generated using itextsharp giving error at the time of first print command

I am getting below for the first time giving print command.

"An error exists on this page. Acrobat may not display the page correctly. please contact the person who created the pdf document to correct the problem".

Print out is comming very fine. and Second time print out command not giving any error.

Please help me why this error is comming for the first time print.

This is part of my code to create PDF

PdfContentByte cb = writer.DirectContent;
cb.BeginText();
Font NormalFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, Color.BLACK);
// Add an image to a fixed position 
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(Server.MapPath("~/images/images/banner.tiff"));
img.SetAbsolutePosition(35, 760);
img.ScalePercent(50);
cb.AddImage(img);
// Draw a line by setting the line width and position
cb.SetLineWidth(2);
cb.MoveTo(20, 740);
cb.LineTo(570, 740);
cb.Stroke();
//Header Details
cb.BeginText();
writeText(cb, drHead["EmpName"].ToString(), 25, 745, f_cb, 14);
writeText(cb, "Employee ID:", 450, 745, f_cn, 12);
writeText(cb, drHead["EmployeeID"].ToString(), 515, 745, f_cb, 12);
cb.EndText();
cb.BeginText();
writeText(cb, "XXXX:", 25, 725, f_cb, 8);
cb.EndText();
cb.SetLineWidth(2);
cb.MoveTo(20, 675);
cb.LineTo(570, 675);
cb.Stroke();
cb.EndText();
// Acknowledgement section
cb.BeginText();
writeText(cb, "XXXXXXXXXXXXXXXX", 20, 140, f_cb, 12);
cb.EndText();
cb.EndText();

Please help me to know what is the issue.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have nested text blocks. That's illegal PDF syntax. I think recent versions of iTextSharp warn you about this, so I guess you're using an old version.

This is wrong:

cb.BeginText();
...
cb.BeginText();
...
cb.EndText();
...
cb.EndText();

This is right:

cb.BeginText();
...
cb.EndText();
...
cb.BeginText();
...
cb.EndText();

Moreover: ISO-32000-1 tells you that some operations are forbidden inside a text block.

This is wrong:

cb.BeginText();
...
cb.AddImage(img);
...
cb.EndText();

This is right:

cb.BeginText();
...
cb.EndText();
...
cb.AddImage(img);

Finally, some operators are mandatory when creating a text block. For instance: you always need setFontAndSize() (I don't know what you're doing in writeText(), but I assume you're setting the font correctly).

In any case: you have chosen to use iTextSharp at the lowest level, writing PDF syntax almost manually. This assumes that you know ISO-32000-1 inside-out. If you don't, you should use some of the high-level objects, such as ColumnText to position content at absolute positions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...