iText PDF image partially displayed - itext

The image is not displayed correctly in my PDF (iText) when I access thru my Java code.
It displays partially with the first half of the image displaying properly and the remaining half displays with a lot of lines on top of the image. (the images seems to be downloading very slow when other text show up fast).
I use iTextPdf version 5.4.0 jar file and I access the image thru URL (get the image URL)
in my java code.
Please let me know why this happens. If you need any additional info please let me know and I can provide.
Thanks in advance for any help.

Im also faced Same issue, later we solved. Please find the below code, hope it will help for you.
HTML FILE
<html>
<body>
<font color="green">Test</font><br/>
<table>
<tr><td><img src="Desert.jpg" height="300" width="300"/></td></tr>
</table>
</body>
</html>
Java File
class PageWithRectangle extends PdfPageEventHelper
{
public void onEndPage(PdfWriter writer, Document document)
{
PdfContentByte cb = writer.getDirectContent();
Rectangle pageSize = writer.getPageSize();
cb.rectangle(pageSize.getLeft() + 3, pageSize.getBottom() + 3,
pageSize.getWidth() - 6, pageSize.getHeight() - 6);
cb.stroke();
}
}
public class pdfTest {
private static String getUrlSource(String url) throws IOException {
URL webpage = new URL(url);
URLConnection yc = webpage.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream(), "UTF-8"));
String inputLine;
StringBuilder a = new StringBuilder();
while ((inputLine = in.readLine()) != null)
{
a.append(inputLine);
System.out.println(inputLine);
}
in.close();
return a.toString();
}
public static void main(String[] args) {
try {
File baseDir = new File(".");
File outDir = new File(baseDir, "out");
outDir.mkdirs();
String k = getUrlSource("file:\\C:\\test.html");
OutputStream file = new FileOutputStream(new File(outDir+"/Test.pdf"));
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, file);
writer.setPageEvent(new PageWithRectangle());
document.open();
HTMLWorker htmlWorker = new HTMLWorker(document);
htmlWorker.parse(new StringReader(k));
document.close();
file.close();
System.out.println("\nSuccess");
} catch (Exception e) {
e.printStackTrace();
}
}
}
My Old HTML Code(Gave wrong pdf while generate through java)
<html>
<body>
<font color="green">Test</font><br/>
<img src="Desert.jpg" height="300" width="300"/>
</body>
</html>
Solution : give image tag under table tag
Regards,
Praveen

I also have experienced this issues using iText 5.5.5 and found the issue affecting GIF's with alpha channel set. Either remove the alpha or try saving as a jpg. This worked for me.

Related

itextpdf generates blurry pictures

I am using itextpdf html2pdf tool to implement freemarker template to generate pdf
Refer to the official documentation link:
https://kb.itextpdf.com/home/it7kb/ebooks/itext-7-converting-html-to-pdf-with-pdfhtml/chapter-7-frequently-asked-questions-about-pdfhtml/can-pdfhtml-render-base64-images-to-pdf
The front end uses echarts to convert the image to base64.
The picture can be displayed clearly on the browser, but after the pdf is generated, the picture becomes blurry.
Refer to html and pdf image comparison:
html pdf
public static void main(String[] args) throws Exception{
File file = FileUtil.newFile("D:/test.html");
BufferedOutputStream outputStream = FileUtil.getOutputStream("D:/test.pdf");
BufferedInputStream inputStream = FileUtil.getInputStream(file);
ConverterProperties converterProperties=new ConverterProperties();
FontProvider fontProvider=new DefaultFontProvider();
FontProgram fontProgram = FontProgramFactory.createFont(Constant.CONFIG_PATH+File.separator+"simsun.ttc,1");
fontProvider.addFont(fontProgram);
MediaDeviceDescription mediaDeviceDescription = new MediaDeviceDescription(MediaType.SCREEN);
mediaDeviceDescription.setResolution(2000f);
converterProperties.setFontProvider(fontProvider);
converterProperties.setMediaDeviceDescription(mediaDeviceDescription);
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outputStream));
pdfDoc.setDefaultPageSize(new PageSize(1000,1700));
HtmlConverter.convertToPdf(inputStream,pdfDoc,converterProperties);
}

Save JavaFX ScrollPane content to PDF file

I'm using the below code to save the content of a ScrollPane in my JavaFx Application to a PDF file.
button.setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
File pdfFile = fileChooser.showSaveDialog(primaryStage);
try {
BufferedImage bufImage = SwingFXUtils.fromFXImage(scrollPane.snapshot(new SnapshotParameters(), null), null);
FileOutputStream out = new FileOutputStream(new File("../temp.jpg"));
javax.imageio.ImageIO.write(bufImage, "jpg", out);
out.flush();
out.close();
com.itextpdf.text.Image image =com.itextpdf.text.Image.getInstance("../temp.jpg");
Document doc = new Document(new com.itextpdf.text.Rectangle(image.getScaledWidth(), image.getScaledHeight()));
FileOutputStream fos = new FileOutputStream(pdfFile);
PdfWriter.getInstance(doc, fos);
doc.open();
doc.newPage();
image.setAbsolutePosition(0, 0);
doc.add(image);
fos.flush();
doc.close();
fos.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
In the scrollpane, I have a very long VBox which contains almost 40-50 labels. So, this code initially saves it to a jpg file and then adds it to a pdf file.
When the temp.jpg is created initially, due to its length, the jpg file looks very thin. It should be zoomed to see the actual content.
When the pdf file is written, it was blank except that it was lengthy as it would have been when the jpg is really converted to a PDF file.
Can anyone help me fix this ? to take the snapshot of the ScrollPane to PDF with its actual size/scale ?
I have first scaled the image and then created document with scaled PageSize. This fixed the issue
com.itextpdf.text.Image image =com.itextpdf.text.Image.getInstance("../temp.jpg");
image.scalePercent(1);
Document doc = new Document(new com.itextpdf.text.Rectangle(image.getScaledWidth(), image.getScaledHeight()));
doc.open();
doc.add(image);

Map System.Drawing.Image to proper PdfName.COLORSPACE and PdfName.FILTER in iTextSharp

I'm using iTextSharp to update an image object in a PDF with a modified System.Drawing.Image. How do I properly set the PdfName.COLORSPACE and PdfName.FILTER based on the System.Drawing.Image? I'm not sure which System.Drawing.Image properties can be used for the mappings.
private void SetImageData(PdfImageObject pdfImage, System.Drawing.Image image, byte[] imageData)
{
PRStream imgStream = (PRStream)pdfImage.GetDictionary();
imgStream.Clear();
imgStream.SetData(imageData, false, PRStream.NO_COMPRESSION);
imgStream.Put(PdfName.TYPE, PdfName.XOBJECT);
imgStream.Put(PdfName.SUBTYPE, PdfName.IMAGE);
imgStream.Put(PdfName.WIDTH, new PdfNumber(image.Width));
imgStream.Put(PdfName.HEIGHT, new PdfNumber(image.Height));
imgStream.Put(PdfName.LENGTH, new PdfNumber(imageData.LongLength));
// Not sure how to properly set these entries based on the image properties
imgStream.Put(PdfName.BITSPERCOMPONENT, 8);
imgStream.Put(PdfName.COLORSPACE, PdfName.DEVICERGB);
imgStream.Put(PdfName.FILTER, PdfName.DCTDECODE);
}
I took the advice of Chris Haas and cheated by writing the System.Drawing.Image to a temporary PDF and then read it back out as a PdfImageObject.
using (MemoryStream ms = new MemoryStream())
{
using (iTextSharp.text.Document doc = new iTextSharp.text.Document())
{
using (iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(doc))
{
doc.Open();
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(drawingImage, drawingImage.RawFormat);
image.SimplifyColorspace();
doc.Add(image);
doc.Close();
}
}
... code that opens the mem stream with PdfReader and retrieves the image as PdfImageObj...
}
That worked but seemed like allot of side stepping for a cheat. I stepped through the code in the call to doc.Add(image) and found that eventually a PdfImage object was created from the iTextSharp.text.Image object and the PdfImage object contained all the dictionary entries that I needed. So I decided to cut some corners on the original cheat and come up with this as my final solution:
private void SetImageData(PdfImageObject pdfImageObj, byte[] imageData)
{
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageData);
image.SimplifyColorSpace();
PdfImage tempPdfImage = new PdfImage(image, "TempImg", null);
PRStream imgStream = (PRStream)pdfImageObj.GetDictionary();
imgStream.Clear();
imgStream.SetDataRaw(imageData);
imgStream.Merge(tempPdfImage);
}

create pdf from asp.net page view

I try to create a pdf file from html page view with itextsharp.
This code block works but, I get html code string from url and I want to give this string to be converted. So I can convert any site view to pdf by giving url to function. Thanks..
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
WebClient wc = new WebClient();
string htmlText = wc.DownloadString("http://");
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
HTMLWorker is buggy and has been discontinued, in favor of XMLWorker.
The project page : http://sourceforge.net/projects/xmlworker/
The demo page : http://demo.itextsupport.com/xmlworker/
The documentation : http://demo.itextsupport.com/xmlworker/itextdoc/index.html
Even though the documentation refers to the Java version, adapting the examples to iTextSharp should be straightforward.

iText AddImage() to specific page

I'm having a problem trying to locate a PdfContentByte directly into an specific page. My problem is: I need to add an Image for each page (That works) and need to add a QRCode to each of the pages at the right bottom corner but this works only for the first Page and I don't know how to repeat it on the other ones.
This is my code:
public string GeneratePDFDocument(Atomic.Development.Montenegro.Data.Entities.Document document, Stamp stamp)
{
string filename = #"C:\Users\Sheldon\Desktop\Pdf.Pdf";
FileStream fs = new FileStream(filename, FileMode.Create);
iTextSharp.text.Document pdfDocument = new iTextSharp.text.Document(PageSize.LETTER, PAGE_LEFT_MARGIN, PAGE_RIGHT_MARGIN, PAGE_TOP_MARGIN, PAGE_BOTTOM_MARGIN);
iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDocument, fs);
pdfDocument.Open();
int count = document.Pages.Count;
foreach (Page page in document.Pages)
{
Image img = Image.GetInstance(page.Image);
img.ScaleToFit(PageSize.LETTER.Width-(PAGE_LEFT_MARGIN + PAGE_RIGHT_MARGIN), PageSize.LETTER.Height-(PAGE_TOP_MARGIN + PAGE_BOTTOM_MARGIN));
pdfDocument.Add(img);
PlaceCodeBar(writer);
}
pdfDocument.Close();
writer.Close();
fs.Close();
return filename;
}
private static void PlaceCodeBar(iTextSharp.text.pdf.PdfWriter writer)
{
String codeText = "TEXT TO ENCODE";
iTextSharp.text.pdf.BarcodePDF417 pdf417 = new iTextSharp.text.pdf.BarcodePDF417();
pdf417.SetText(codeText);
Image img = pdf417.GetImage();
iTextSharp.text.pdf.BarcodeQRCode qrcode = new iTextSharp.text.pdf.BarcodeQRCode(codeText, 1, 1, null);
img = qrcode.GetImage();
iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
cb.SaveState();
cb.BeginText();
img.SetAbsolutePosition(PageSize.LETTER.Width-PAGE_RIGHT_MARGIN-img.ScaledWidth, PAGE_BOTTOM_MARGIN);
cb.AddImage(img);
cb.EndText();
cb.RestoreState();
}
So add it in your foreach (Page...) loop:
foreach (Page page in document.Pages)
{
Image img = Image.GetInstance(page.Image);
img.ScaleToFit(PageSize.LETTER.Width-(PAGE_LEFT_MARGIN + PAGE_RIGHT_MARGIN), PageSize.LETTER.Height-(PAGE_TOP_MARGIN + PAGE_BOTTOM_MARGIN));
pdfDocument.Add(img);
PlaceCodeBar(writer);
}
If this is a second pass on the same PDF (you've closed it then opened it again), use a PdfStamper rather than a PdfWriter. You can then get the direct content of each page rather than the one direct content that is reused (and reset) for each page.
PS: Drop the BeginText() and EndText() calls. Those operators should only be used when actually drawing text/setting fonts/etc. No line art. No images. The SaveState()/RestoreState() are good though. Definitely keep those.
I just figure out how to solve the problem. Just delete the cb.SaveState() and cb.RestoreState() and it put the image on the page is actually active.