Comments (3)
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.
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.
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)
- some example projects in wiki are missing HOT 2
- Streams objects writing problem HOT 2
- Add watermark to PDF HOT 7
- Missing lib.obj file HOT 3
- Android Build Workflow HOT 3
- CIDSet encoding does not conform with ISO 19005-2:2011, ISO 19005-3:2012 (PDF/A-2b or PDF/A-3b) HOT 21
- annotations are lost with PDFDocumentCopyingContext::AppendPDFPageFromPDF HOT 3
- How to draw Bezier curves using PDF-Witer library? HOT 2
- Parse a screenplay into scene objects? HOT 2
- color emojis HOT 16
- Links are removed when documents are merged HOT 8
- Color inversion problem occurs when exporting images HOT 1
- infinite loop HOT 2
- Crash when WriteUsedFontsDefinitions HOT 17
- Publish to github releases without PDFWriterTesting HOT 4
- U3D support, 10 years later HOT 10
- `Segmentation fault (core dumped)` just for adding `PDFWriter pdfWriter` in the `h` file HOT 11
- Fail to draw rectangle to pdf,if rectangle larger than(595x842) HOT 2
- german umlaute diacritic not rendered properly HOT 2
- Make a PDF with an image in memory HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pdf-writer.