Svg Image is not properly render in the pdf - java

[]
1[]2]3
Here is my code to add svg image in the pdf using itext 7.1.3 and also using SVGConventer.The image not properly rendered, i am not able understand where and what is the problem? Can anyone help me. Thanks in advance.
PdfWriter writer = new PdfWriter(new FileOutputStream("/home/users/Documents/pdf/new.pdf"));
PdfDocument pdfDoc = new PdfDocument(writer);
Document doc = new Document(pdfDoc);
URL svgUrl = new File(svg).toURI().toURL();
doc.add(new Paragraph("new pikachu"));
Image image = SvgConverter.convertToImage(svgUrl.openStream(), pdfDoc);
doc.add(image);
doc.close();

Related

iText html2pdf change size of the output PDF

How to change output PDF page size when converting it from HTML with iText html2pdf library? Tried this code:
ConverterProperties properties = new ConverterProperties();
MediaDeviceDescription description = MediaDeviceDescription.createDefault();
description.setHeight(1024);
description.setWidth(1024);
properties.setMediaDeviceDescription(description);
HtmlConverter.convertToPdf(new File(htmlSource), new File("outputFile.pdf"), properties);
but, looks like it does not work, my output page is still A4
Use one of the HtmlConverter's methods which take a PdfDocument instance as a parameter and set the needed page size on it with setDefaultPageSize method.
For example,
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(destinationPath));
pdfDoc.setDefaultPageSize(new PageSize(1500, 842));
HtmlConverter.convertToPdf(new FileInputStream(sourcePath), pdfDoc);

How to render part of a PDF file as an image using PDFBox?

PDFBox offer functions to render a entire page, but no way to render only a specific rectangle of the page.
This code is working for me. But as mentioned above it renders the whole page and I need a method to render a little area of the pdf file:
File file = new File("package.pdf");
PDDocument document = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(document);
BufferedImage image = renderer.renderImageWithDPI(0, 400);
ImageIO.write(image, "PNG", new File("C:/package1.png"));
document.close();
I would be very happy about a solution, since I have not found a solution for hours
I have found a soulution by myself. CropBox was the deciding keyword i did not know about.
File file = new File("package.pdf");
PDDocument document = PDDocument.load(file);
PDPage page = document.getPage(0);
page.setCropBox(new PDRectangle(133f, 150f, 100f, 100f)); // Here you draw a rectangle around the area you want to specify
PDFRenderer renderer = new PDFRenderer(document);
BufferedImage image = renderer.renderImageWithDPI(0, 400);
ImageIO.write(image, "PNG", new File("C:/fatihabi.png"));
document.close();

How to save charts in same pdf file using Xchart and VectorGraphics2D

I create chart using Xchart and save it as PDF file using VectorGraphics2D as follows:
VectorGraphicsEncoder.saveVectorGraphic(chart, "chart_name", VectorGraphicsFormat.PDF);
However I need to save several charts in the same PDF file. Is there any way of doing it using iText?
ProcessingPipeline g = null;
g = new PDFGraphics2D(0.0, 0.0, chart.getWidth(), chart.getHeight());
chart.paint(g, chart.getWidth(), chart.getHeight());
FileOutputStream file1 = new FileOutputStream(addFileExtension("doc_name", VectorGraphicsFormat.PDF));
Document doc = new Document(PageSize.A4.rotate());
PdfWriter writer = PdfWriter.getInstance(doc, file1);
doc.open();
However I fail when I add the g to the document. How do I fix this?
You can save an image with desired grid in one image using :
BitmapEncoder.saveBitmap(
Arrays.asList(new Chart[]{chart1, chart2, chart3, chart4}), 2,2, "./cumulative", BitmapEncoder.BitmapFormat.PNG);
where 2,2 is the row and col size.
After getting this image with 4 charts you can export it in a PDF.

how to solve read only file system error in android programming with eclipse

I am developing a program in which i must create pdf file inside application.
This is the code that i use for creating pdf file but an error occurred that say
"/Image.pdf:open failed:EROFS(read-only file system)"
This is my button click code:
Document document = new Document();
PdfWriter.getInstance(document,new FileOutputStream("Image.pdf"));
document.open();
Image image1 = Image.getInstance("watermark.png");
document.add(image1);
String imageUrl = "http://jenkov.com/images/20081123-20081123-3E1W7902-small-portrait.jpg";
Image image2 = Image.getInstance(new URL(imageUrl));
document.add(image2);
document.close();
Try to write pdf in sdcard root directory:
PdfWriter.getInstance(document,new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"Image.pdf"));
first i thank Haresh Chhelana for his answers.
by using this code i can create new pdf file with an image in it.
Document document = new Document();
PdfWriter.getInstance(document,new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Image.pdf"));
document.open();
Image image1 = Image.getInstance(Environment.getExternalStorageDirectory()+"/01.jpg");
document.add(image1);
document.close();

Pdf not generated using iText PDF when image not found

Code is working well when image is found on said path.
Code:
...
Document document=new Document();
PdfWriter.getInstance(document,new FileOutputStream("D:/pdfSample.pdf"));
document.open();
String text="<html><body>hello<img src='http://pathOfImage/Image.gif' alt='' /></body></html>";
HTMLWorker htmlWorker = new HTMLWorker(document);
htmlWorker.parse(new StringReader(text));
document.close();
System.out.println( "PDF Created!" );
But if image is not found, pdf is not getting generated i.e. image alt property is not working. What can be done?

Categories

Resources