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);
}
}