I have documents that are rotated (90, 180 and 270) and I’ve been having several issues positioning objects properly as the coordinates system is inconsistent and some cases, the element even disappear.
In short, I need to draw a rectangle (opaque) where there is a label and an icon on the top right corner of it.
The sample code below highlights the two main issues I’ve been facing:
- When I rotate the text fragment, the text disappear as the inner rectangle is not rotating along with it (even if I update the rectangle coordinates manually).
- The image coordinate system is completely different from the floating box with text coordinates, somehow the position 0x0 is out of the page.
package com.csdisco.publications.pdf.decorate;
import com.aspose.pdf.*;
import com.csdisco.publications.pdf.decorate.util.Licenser;
import com.csdisco.shared.pdf.aspose.processing.ProcessingEngine;
import com.csdisco.shared.pdf.aspose.processing.doc.impl.PdfProcessingDocument;
import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.*;
import java.util.Locale;
public class Main2 {
public static void main(String[] args) throws IOException {
var doc = new PdfProcessingDocument("Ferrari 308 Quattrovalvole, 328 GTB, 328 GTS Workshop Manual.pdf");
var image = new Image();
image.setFixWidth(12);
image.setFixHeight(12);
image.setImageStream(createImage());
var floatingBox = new FloatingBox(50, 50);
floatingBox.setLeft(0);
floatingBox.setTop(560);
floatingBox.getParagraphs().add(image);
doc.getPage(3).getPageInfo().setMargin(new MarginInfo(0,0,0,0));
doc.getPage(3).getParagraphs().add(floatingBox);
var floatingBox2 = new FloatingBox();
var textFragment = new TextFragment("Description");
textFragment.getTextState().setRotation(180);
textFragment.getTextState().setFont(FontRepository.findFont("Arial"));
textFragment.getTextState().setFontSize(20);
textFragment.getTextState().setForegroundColor(Color.getBlack());
textFragment.getTextState().setBackgroundColor(Color.getRed());
floatingBox2.getParagraphs().add(textFragment);
floatingBox2.setTop(560);
floatingBox2.setLeft(100);
doc.getPage(3).getParagraphs().add(floatingBox2);
doc.getDocument().save("test2.pdf");
}
private static InputStream createImage() throws IOException {
try (InputStream is = Spike4.class.getClassLoader().getResourceAsStream("annotation-multiple.png");
InputStream imageStream = new ByteArrayInputStream(is.readAllBytes());
ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
BufferedImage originalImage = ImageIO.read(imageStream);
BufferedImage argbImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics2D createGraphics = argbImage.createGraphics();
createGraphics.drawImage(originalImage, 0, 0, originalImage.getWidth(), originalImage.getHeight(), null);
// Preserve the RGB, just change opacity (Alpha).
RescaleOp r = new RescaleOp(new float[]{1f, 1f, 1f, 255}, new float[]{0, 0, 0, 0}, null);
BufferedImage transparentImage = r.filter(argbImage, null);
// GIF or PNG are the formats that support transparency. PNG has higher definition overall, so using it.
ImageIO.write(transparentImage, "png", baos);
createGraphics.dispose();
return new ByteArrayInputStream(baos.toByteArray());
}
}
}
Document being used can be found here: FERRARI 328 GTB WORKSHOP MANUAL Pdf Download | ManualsLib
Can you please clarify how does the coordinate system works? Additionally, is there a way to “discard” the rotation, preserving the content as-is?