PDF-Writer icon indicating copy to clipboard operation
PDF-Writer copied to clipboard

Retrieve Page Dimensions

Open gnat42 opened this issue 7 years ago • 1 comments

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?

gnat42 avatar Oct 25 '17 20:10 gnat42

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?

gnat42 avatar Oct 25 '17 21:10 gnat42