Giter Club home page Giter Club logo

Comments (3)

galkahana avatar galkahana commented on July 24, 2024 1

hmmm. I'd prefer to try the code out before replying, but i might be tied up this week. I think what might be a problem is that the form is created after StartContentContext. Try moving the CreateFormXObjectFromJPGStream call to before StartContentContext. you can even just save the object id of the form and delete the pdfFormXObject immediately. I think this should work better.

If i recall correctly writing other objects while writing the stream object that is the content stream would disrupt the currently written content stream...hence the problem.

from pdf-writer.

totravel avatar totravel commented on July 24, 2024 1

It works! Thank you very much!

Below is the final code:

PDFWriter pdfWriter;
if (pdfWriter.ModifyPDF("a.pdf", ePDFVersion13, "") != PDFHummus::eSuccess) {
  std::cerr << "failed to modify PDF\n";
  return -1;
}


PDFModifiedPage pdfModifiedPage(&pdfWriter, nPageIndex);


const cv::Mat img = cv::imread("a.jpg");
if (!img.empty()) {

  std::vector<unsigned char> buf;
  if (cv::imencode(".jpg", img, buf)) {

    InputByteArrayStream* inBuf = new InputByteArrayStream(buf.data(), buf.size());
    PDFFormXObject* pdfFormXObject = pdfWriter.CreateFormXObjectFromJPGStream(inBuf);
    if (pdfFormXObject != nullptr) {

      AbstractContentContext* pdfPageCtx = pdfModifiedPage.StartContentContext();
      if (pdfPageCtx != nullptr) {

        const std::string& pdfFormXObjectName =
          pdfModifiedPage.GetCurrentResourcesDictionary()->AddFormXObjectMapping(pdfFormXObject->GetObjectID());

        pdfPageCtx->q();
        pdfPageCtx->cm(1, 0, 0, 1, 0, 0);
        pdfPageCtx->Do(pdfFormXObjectName);
        pdfPageCtx->Q();

        pdfModifiedPage.EndContentContext();
      }
      else {
        std::cerr << "failed to create content context for page\n";
      }

      delete pdfFormXObject;
    }
    else {
      std::cerr << "failed to create form XObject from stream\n";
    }
  }
  else {
    std::cerr << "failed to encode image\n";
  }
}
else {
  std::cerr << "failed to read image from file\n";
}


pdfModifiedPage.WritePage();
pdfWriter.EndPDF();

from pdf-writer.

galkahana avatar galkahana commented on July 24, 2024

Alternatively, you could use the content context method pdfPageCtx ->DrawImage(....) which can take care of remembering to write the image form after the page is ended. (before or after doesn't matter...just as long as it's not during, which is what happens once you call StartContentContext...assuming this is the problem.

from pdf-writer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.