galkahana / PDF-Writer

High performance library for creating, modiyfing and parsing PDF files in C++
http://www.pdfhummus.com
Apache License 2.0
904 stars 217 forks source link

PDF page Extents are too Small #165

Open ghernando opened 5 years ago

ghernando commented 5 years ago

I am new to the project and would like to be able to stamp all the pages of a PDF with some text. I was following some of the C++ examples that use ModifyPDF. I expected the MediaBox to be the extents of the PDF page, but it seems to be very small. The size of the sample file I have reports a MediaBox to be 2016 x 2880. But the actual page seems to be much bigger. I think maybe I am not correctly acquiring the page. Could you please suggest what I might be doing wrong?

testfile.pdf ` pdfWriter.ModifyPDF("./testfile.pdf", ePDFVersion15, "", LogConfiguration(true, true, "SimpleTextUsage.log", PDFCreationSettings(true, inEmbedFonts));

    PDFUsedFont* f = pdfWriter.GetFontForFile(
        RelativeURLToLocalPath(inTestConfiguration.mSampleFileBase,
        "TestMaterials/fonts/arial.ttf"));
    PDFParser& modifiedFileParser = pdfWriter.GetModifiedFileParser();

    for (unsigned long i = 0; i < modifiedFileParser.GetPagesCount(); ++i)
    {
        ObjectIDType PageID = modifiedFileParser.GetPageObjectID(i);
        PDFObject* page = modifiedFileParser.ParsePage(i);

        PDFModifiedPage modifiedPage(&pdfWriter, i);

        PageContentContext* contentContext = (PageContentContext*)modifiedPage.StartContentContext();
        AbstractContentContext::TextOptions opt(f, 50, AbstractContentContext::eRGB, 0xFF45DE);

        contentContext->WriteText(75, 105, "Test Text that is supercalofragolisticly long....  I mean really long to see if it goes off the page...", opt);

        PDFPageInput pageInput = PDFPageInput(&modifiedFileParser, page);
        PDFRectangle cropBox = pageInput.GetCropBox();
        PDFRectangle mediaBox = pageInput.GetMediaBox();
        contentContext->DrawRectangle(cropBox.LowerLeftX, cropBox.LowerLeftY, cropBox.UpperRightX, cropBox.UpperRightY);
        cout << mediaBox.UpperRightX - mediaBox.LowerLeftX << " " << mediaBox.UpperRightY - mediaBox.LowerLeftY << "\n";

        modifiedPage.EndContentContext();
        modifiedPage.WritePage();
    }
    status = pdfWriter.EndPDF();`
HaerBugMaker commented 7 months ago

Hello,I meet the same problem,Did you solve it.Looking forward to your reply,:)