Scale/Resize Converted Content

The expected behavior:
1- convert word document to PDF
2- change page size to bigger size after conversion
3- scale up the converted word page size to fill more area in the resized pdf page
4- move the converted word page to the right and upwards to match the design

both step 1 and 2 are working as expected, step 3 and 4 works but doesn’t give expected results.

the expected result is something similar to the following:
image.png (4.4 KB)

the output looks like this:
image.png (51.6 KB)

PS: the issue is not with the border, it’s about the size and position of the converted word page.

I’ve attached the original word document:
file-sample_1MB.docx (1003 KB)

the code:

  @Test
  public void convertWord() throws Exception {

    try (InputStream is = wordDocument.getInputStream()) {
      com.aspose.words.Document word = new com.aspose.words.Document(is);

      for (Section section : word.getSections()) {
        section.getPageSetup().setLineStartingNumber(1);
        section.getPageSetup().setLineNumberCountBy(1);
        section.getPageSetup().setLineNumberRestartMode(LineNumberRestartMode.RESTART_PAGE);

        section.getPageSetup().getBorders().setColor(Color.black);
        section.getPageSetup().getBorders().setLineStyle(LineStyle.DOUBLE_WAVE);
      }

      try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {

        // save the word to in memory output stream
        word.save(os, com.aspose.words.SaveFormat.PDF);

        try (ByteArrayInputStream pdfIS = new ByteArrayInputStream(os.toByteArray())) {

          com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(pdfIS);

          // set the page size and rectangles sizes to 918 * 1188
          pdfDocument.getPages().forEach(page -> setPageSize(918, 1188, page));

          PdfFileEditor pdfFileEditor = new PdfFileEditor();


          // scale the converted content
          ContentsResizeValue width = ContentsResizeValue.units(798);
          ContentsResizeValue height = ContentsResizeValue.units(984);
          ContentsResizeValue margin60 = ContentsResizeValue.units(60);
          ContentsResizeValue margin30 = ContentsResizeValue.units(30);

          ContentsResizeParameters parameters = new ContentsResizeParameters(
              margin30,
              width,
              margin30,
              margin60,
              height,
              margin60
          );

          // perform the resize
          pdfFileEditor.resizeContents(pdfDocument, parameters);

          // add page size stamp for debugging
          pdfDocument.getPages().forEach(page -> {
            String stamp = String.format("width: %s height: %s mbX: %s mbY: %s",
                page.getPageInfo().getWidth(),
                page.getPageInfo().getHeight(),
                page.getRect().getWidth(),
                page.getRect().getHeight());

            TextStamp textStamp = new TextStamp(stamp);
            textStamp.setVerticalAlignment(VerticalAlignment.Top);
            textStamp.setHorizontalAlignment(VerticalAlignment.Center);
            textStamp.setTopMargin(20);

            page.addStamp(textStamp);

          });

          // save the modified pdf document to file system
          writeToFile(pdfDocument);

        }
      }

    }

  }

  static void setPageSize(float width, float height, Page convertedPage) {
    convertedPage.getPageInfo().setLandscape(false);
    convertedPage.setPageSize(width, height);
    convertedPage.getPageInfo().setWidth(width);
    convertedPage.getPageInfo().setHeight(height);
    convertedPage.setMediaBox(new Rectangle(0, 0, width, height));
    convertedPage.setArtBox(new Rectangle(0, 0, width, height));
    convertedPage.setBleedBox(new Rectangle(0, 0, width, height));
    convertedPage.setTrimBox(new Rectangle(0, 0, width, height));
    convertedPage.setRect(new Rectangle(0, 0, width, height));
  }

  private static void writeToFile(com.aspose.pdf.Document pdfDocument) throws IOException {
    Path path = Paths.get("/home/lqutom/test123456.pdf");
    try (OutputStream fos = (Files.newOutputStream(path,
        StandardOpenOption.WRITE, StandardOpenOption.CREATE))) {
      pdfDocument.save(fos, SaveFormat.Pdf);
    }
  }

@lqutom,

I cannot open the word attachment. It said the file does not match the extension docx:

Please change it to .doc I had to change it to .docx as your forum didn’t accept .doc extension.

@lqutom,

After testing multiple times it seem the issue is with the SetPageSize method per page.

If you only do that you will see the content get resized in an abnormal factor to the original size. I will be creating a ticket.

@lqutom
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-53782

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@lqutom

In this case, you can use the code snippet:

//Aspose.Words starts here
var wordDoc = new Aspose.Words.Document(dataDir + "53782.doc");
foreach (Aspose.Words.Section section in wordDoc.Sections)
{
    section.PageSetup.LineStartingNumber = 1;
    section.PageSetup.LineNumberCountBy = 1;
    section.PageSetup.LineNumberRestartMode = Aspose.Words.LineNumberRestartMode.RestartPage;
    section.PageSetup.Borders.Color = System.Drawing.Color.Black;
    section.PageSetup.Borders.LineStyle = Aspose.Words.LineStyle.DoubleWave;
}
using (var stream = new System.IO.FileStream(dataDir + "53782_temp.pdf", System.IO.FileMode.Create))
{
    wordDoc.Save(stream, Aspose.Words.SaveFormat.Pdf);
    //Aspose.Words ends here
}
//Aspose.Pdf starts here
//Change Size
{
    var pdfOrg = new PdfOrganizer();
    var conf = new PdfOrganizerResizeOptions();
    conf.PageSize = new Aspose.Pdf.PageSize(798, 984);
    conf.AddDataSource(new FileDataSource(dataDir + "53782_temp.pdf"));//Can use StreamDataSource
    conf.AddSaveDataSource(new FileDataSource(dataDir + "53782_org.pdf"));
    pdfOrg.Process(conf);
}
{
    //add page size stamp for debugging
    var pdfDoc = new Aspose.Pdf.Document(dataDir + "53782_org.pdf");
    foreach (var page in pdfDoc.Pages)
    {
        string stamp = $"width: {page.PageInfo.Width} height: {page.PageInfo.Height} mbX: {page.Rect.Width} mbY: {page.Rect.Height} LLX: {page.Rect.LLX} LLY:{page.Rect.LLY} URX:{page.Rect.URX} URY:{page.Rect.URY}";
        var textStamp = new Aspose.Pdf.TextStamp(stamp);
        textStamp.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Top;
        textStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
        textStamp.TopMargin = 20;
        page.AddStamp(textStamp);
    }
    pdfDoc.Save(dataDir + "53782_org_stamp.pdf");
}

@asad.ali we’re using aspose total Java api.

there doesn’t seem to be ‘PdfOrganizer’ class in the java API.

@kperry1

Please make sure to use the latest version and you should be able to access this class as Aspose.Pdf.Plugins.PdfOrganizer.

I’m using Aspose PDF for Java 23.8 and the class doesn’t exist.

the package com.aspose.pdf.plugins doesn’t exist neither.

@kperry1

This would be available in 23.9 version of the Java API that will be released soon. However, please feel free to let us know in case you still face any issues while using it.