You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Jessy Ping <te...@gmail.com> on 2021/05/29 10:41:12 UTC
Converting pptx to pdf
HI all,
I am trying to convert pptx file containing a custom theme and two text
boxes to pdf. I am able to convert into PDF, but the theme size is reduced
and the two text box is printed on a white background.Any insights will be
helpful.
Code below,
static void convertToPdf(String fileName) throws IOException,
DocumentException {
> FileInputStream inputStream = new FileInputStream(pptLocation + fileName + PPT);
> double zoom = 2;
> AffineTransform at = new AffineTransform();
> at.setToScale(zoom, zoom);
> Document pdfDocument = new Document();
> PdfWriter pdfWriter = PdfWriter.getInstance(pdfDocument, new FileOutputStream(pdfLocation + fileName + PDF));
> PdfPTable table = new PdfPTable(1);
> pdfWriter.open();
> pdfDocument.open();
> Dimension pgsize = null;
> Image slideImage = null;
> BufferedImage img = null;
>
>
> XMLSlideShow ppt = new XMLSlideShow(inputStream);
> pgsize = ppt.getPageSize();
> List<XSLFSlide> slides = ppt.getSlides();
> pdfDocument.setPageSize(new Rectangle((float) pgsize.getWidth(), (float) pgsize.getHeight()));
> pdfWriter.open();
> pdfDocument.open();
> for (int i = 0; i < slides.size(); i++) {
> img = new BufferedImage((int) Math.ceil(pgsize.width * zoom), (int) Math.ceil(pgsize.height * zoom), BufferedImage.TYPE_INT_RGB);
> Graphics2D graphics = img.createGraphics();
> graphics.setTransform(at);
>
> graphics.setPaint(Color.white);
> graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
> slides.get(i).draw(graphics);
> graphics.getPaint();
> slideImage = Image.getInstance(img, null);
> table.addCell(new PdfPCell(slideImage, false));
>
> }
> pdfDocument.add(table);
> pdfDocument.close();
> pdfWriter.close();
>
> }
>
>
Re: Converting pptx to pdf
Posted by Andreas Beeker <ki...@apache.org>.
Hi Jessy,
here a few points about this issue:
* PDF support is relatively new and there are a few/lot (?) of cases which work differently opposed to PPTX->PNG
* I can have a look at your input file and try to figure out what's wrong - either send it to the list or to me privately
* Cascaded properties from themes are a source of errors - POIs handling is a bit shirtsleeved in this area
* Please try out the convenience tool PPTX2PNG (http://poi.apache.org/components/slideshow/ppt-wmf-emf-renderer.html)
* Also check the nightly - description about how to use the nightly is also in the above link. In case I fix something, it would be cool, if you could verify it
Andi
On 29.05.21 12:41, Jessy Ping wrote:
> HI all,
>
> I am trying to convert pptx file containing a custom theme and two text
> boxes to pdf. I am able to convert into PDF, but the theme size is reduced
> and the two text box is printed on a white background.Any insights will be
> helpful.
> Code below,
>
> static void convertToPdf(String fileName) throws IOException,
> DocumentException {
>> FileInputStream inputStream = new FileInputStream(pptLocation + fileName + PPT);
>> double zoom = 2;
>> AffineTransform at = new AffineTransform();
>> at.setToScale(zoom, zoom);
>> Document pdfDocument = new Document();
>> PdfWriter pdfWriter = PdfWriter.getInstance(pdfDocument, new FileOutputStream(pdfLocation + fileName + PDF));
>> PdfPTable table = new PdfPTable(1);
>> pdfWriter.open();
>> pdfDocument.open();
>> Dimension pgsize = null;
>> Image slideImage = null;
>> BufferedImage img = null;
>>
>>
>> XMLSlideShow ppt = new XMLSlideShow(inputStream);
>> pgsize = ppt.getPageSize();
>> List<XSLFSlide> slides = ppt.getSlides();
>> pdfDocument.setPageSize(new Rectangle((float) pgsize.getWidth(), (float) pgsize.getHeight()));
>> pdfWriter.open();
>> pdfDocument.open();
>> for (int i = 0; i < slides.size(); i++) {
>> img = new BufferedImage((int) Math.ceil(pgsize.width * zoom), (int) Math.ceil(pgsize.height * zoom), BufferedImage.TYPE_INT_RGB);
>> Graphics2D graphics = img.createGraphics();
>> graphics.setTransform(at);
>>
>> graphics.setPaint(Color.white);
>> graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
>> slides.get(i).draw(graphics);
>> graphics.getPaint();
>> slideImage = Image.getInstance(img, null);
>> table.addCell(new PdfPCell(slideImage, false));
>>
>> }
>> pdfDocument.add(table);
>> pdfDocument.close();
>> pdfWriter.close();
>>
>> }
>>
>>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org