PDF-Writer
PDF-Writer copied to clipboard
Retrieve Page Dimensions
Hello, We have a use case where we want users to be able to place text on a PDF. Long story short, we get the coordinates from the user, but need a way to translate them to the PDF co-ordinate system. If I assume a 8.5x11 paper I can hard-code it but that could cause me grief if someone uses a PDF of a different size. So I'm wondering given a PDFWriter or Page object is there a way to get the page dimensions?
So I dug a little more from the original example of modifying a PDF and came up with this:
PDFWriter pdfWriter;
pdfWriter.ModifyPDF("/home/gnat/Projects/pdf-writer-test/Form.pdf",ePDFVersion14,"/home/gnat/Projects/pdf-writer-test/Output.pdf");
PDFModifiedPage modifiedPage(&pdfWriter,0,true);
AbstractContentContext* contentContext = modifiedPage.StartContentContext();
AbstractContentContext::TextOptions opt(pdfWriter.GetFontForFile("/home/gnat/Projects/pdf-writer-test/fonts/arial.ttf"),12,AbstractContentContext::eRGB, 0);
PDFObject* page = pdfWriter.GetModifiedFileParser().ParsePage(0);
if(page) {
PDFRectangle mediaBox = PDFPageInput(&pdfWriter.GetModifiedFileParser(), page).GetMediaBox();
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
cout << "LowerLefX:" << fixed << mediaBox.LowerLeftX << endl;
cout << "LowerLefY:" << fixed << mediaBox.LowerLeftY << endl;
cout << "UpperRightX:" << fixed << mediaBox.UpperRightX << endl;
cout << "UpperRightY:" << fixed << mediaBox.UpperRightY << endl;
}
contentContext->WriteText(0,780,"TEST TEXT", opt);
...
Is this the correct way to do what I want? No other shortcuts via the modified Page etc?