Scale Converted Content To FloatingBox

Dear Aspose,

We’re converting Word documents to PDF and trying combine multiple files into one PDF document.

The requirement to fit converted content into a box to add border and fixed line numbering like this design:

What I tried to do for starters is to get the converted word document, iterate through pages and add their paragraphs into the new PDF paragraphs of a FloatingBox. But it doesn’t work as the paragraphs of the converted document is empty.

Code sample:

    try (os) {
      new com.aspose.words.Document(is).save(os, SaveFormat.PDF);
    }
    try (InputStream in = pipe.createInputStream(os);
        com.aspose.pdf.Document document = new com.aspose.pdf.Document(in)) {
      PageCollection pages = document.getPages();
      pages.forEach(convertedPage -> {

        Page newPdfPage = pdfDocument.getPages().add();

        newPdfPage.getParagraphs().add(new TextFragment("This is a converted file"));

        FloatingBox contentBox = new FloatingBox();
        contentBox.setHorizontalAlignment(HorizontalAlignment.Right);
        contentBox.setWidth(newPdfPage.getPageInfo().getWidth() - 120);
        contentBox.setBorder(new BorderInfo(3, Color.getYellow()));

        //copy paragraphs from converted word to floating box
        convertedPage.getParagraphs().forEach(pr -> contentBox.getParagraphs().add(pr));

        newPdfPage.getParagraphs().add(contentBox);

      });

The result look like this even though the conversion is successful:

Please advise on what’s the best way to achieve this result.
Thanks

@lqutom It looks like you question more related to Aspose.PDF. But wouldn’t it easier to add line numbers upon conversion the document to PDF? For example see the following code:

Document doc = new Document("C:\\Temp\\in.docx");
for (Section s : doc.getSections())
{
    s.getPageSetup().setLineStartingNumber(1);
    s.getPageSetup().setLineNumberCountBy(1);
    s.getPageSetup().setLineNumberRestartMode(LineNumberRestartMode.RESTART_PAGE);
}
doc.save("C:\\Temp\\out.pdf");

in.docx (18.6 KB)
out.pdf (29.5 KB)

In this case it will be required only add header on each page as in your design.

@alexey.noskov thanks for the enlightenment about modifying the original word document to add line numbers.

What’s remaining now is mostly to the PDF team on how to scale content within a bordered box.

Thanks

@lqutom I will move your request to Aspose.PDF forum. My colleagues will help you shortly.

Any updates?

@lqutom

We are looking into this scenario and will get back to you shortly.

@lqutom

Below is a sample code snippet that can be used to add page border in PDF document. You can please check it and modify it as per your requirements:

Document pdf = new Document();
Page page = pdf.getPages().add();

page.getPageInfo().getMargin().setLeft(10);
page.getPageInfo().getMargin().setRight(10);
page.getPageInfo().getMargin().setTop(10);
page.getPageInfo().getMargin().setBottom(10);

int borderWidth = (int) (page.getPageInfo().getWidth());
int borderHeight = (int) (page.getPageInfo().getHeight());

BufferedImage image = new BufferedImage(borderWidth, borderHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graph = image.createGraphics();
graph.fillRect(0, 0, borderWidth, borderHeight);
graph.setColor(java.awt.Color.RED);

//---------- dashed ----------
BasicStroke dashed = new BasicStroke(5, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, new float[]{10.0f}, 0.0f);
graph.setStroke(dashed);

//---------- dotted ----------
//BasicStroke dotted = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] {1,2}, 0);

//graph.setStroke(dotted);
graph.drawRect(0, 0, borderWidth - 1, borderHeight - 1);

BufferedImage stampImage = ImageIO.read(new File(mypng.png"));
graph.drawImage(stampImage, borderWidth / 2 - stampImage.getWidth() / 2, borderHeight / 2 - stampImage.getHeight() / 2, null);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "bmp", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();

ImageStamp imageStampBorder = new ImageStamp(new ByteArrayInputStream(imageInByte));
imageStampBorder.setHorizontalAlignment(HorizontalAlignment.Center);
imageStampBorder.setVerticalAlignment(VerticalAlignment.Center);

TextStamp theStamp = new TextStamp("Hello World");
theStamp.setTextAlignment(HorizontalAlignment.Center);
theStamp.setBackground(false);
theStamp.setOpacity(.5);
theStamp.setRotateAngle(0);
theStamp.setHorizontalAlignment(HorizontalAlignment.Center);
theStamp.setVerticalAlignment(VerticalAlignment.Center);

page.addStamp(imageStampBorder);
page.addStamp(theStamp);

TextFragment text = new TextFragment("Aspose.Pdf for Java 11.5");

for (int i = 0; i < 80; i++) {
page.getParagraphs().add(text);
}

pdf.processParagraphs();
pdf.save("test_stampout.pdf");

Please feel free to let us know in case you still notice any issues.