Comments (2)
Hi,
I think that what you are doing is mostly what should be done. There's some usages of StartPDFStream
with a dictionary in PDFWriter itself, that if im comparing to have a slight difference. For exmaple, here's code for writing CFF fonts that uses StartPDFStream in the same manner you're trying to use it:
https://github.com/galkahana/PDF-Writer/blob/master/PDFWriter/CFFEmbeddedFontWriter.cpp#L95
The key difference is that creating the dictionary context is done via the ObjectsContext::StartDictionary
method as opposed to creating a new dictionary object directly. so, change your code to this for it to work:
DictionaryContext *dictionaryContext = objectsContext->StartDictionary();
dictionaryContext->WriteKey("Type");
dictionaryContext->WriteNameValue("EmbeddedFile");
PDFStream *pdfStream = objectsContext.StartPDFStream(dictionaryContext);
IByteWriter *mwriter = pdfStream->GetWriteStream();
mwriter->Write(inAttachment->FileContent, inAttachment->Lenth);
objectsContext.EndPDFStream(pdfStream);
The reason for the mishap in the code you ran is that dictionaries must be registered at ObjectsContext in an attempt to make starting and ending dictionary consistent (it makes sure that StartDictionary and EndDictionary are called on the same dictionary in case of nesting). StartPDFStream calls EndDictionary, but since the dictionary in the code sample you provided is NOT registered in ObjectsContext there's a failure and so the dictionary end writing is skipped. StartPDFStream does not seem to check the return status code from EndDictionary (there's only a log if you activate logging) which is why it slilengtly continues to just writing the stream keyword and the rest of the stream. (here's some documentation about StartDictionary/EndDictionary in more documentation about the ObjectsContext - https://github.com/galkahana/PDF-Writer/wiki/The-ObjectsContext-Object#dictionary-writing)
i'm gonna fix StartPDFStream to fail if EndDictionary doesn't end well and return null stream pointer...which would very loudly indicate a problem.
from pdf-writer.
yes! With your help, I made it!
In fact, I tried to use objectsContext.StartDictionary()
to create DictionaryContext, but I always used this function objectsContext.EndDictionary(DictionaryContext* inDictionaryContext)
by mistake, which would lead to Segmentation fault
error.
like this.
result.second = objectsContext.StartNewIndirectObject();
DictionaryContext *dictionaryContext = objectsContext.StartDictionary();
dictionaryContext->WriteKey("Type");
dictionaryContext->WriteNameValue("EmbeddedFile");
objectsContext.EndDictionary(dictionaryContext); // my mistake
PDFStream *pdfStream = objectsContext.StartPDFStream(dictionaryContext);
IByteWriter *mwriter = pdfStream->GetWriteStream();
mwriter->Write(inAttachment->FileContent, inAttachment->Lenth);
objectsContext.EndPDFStream(pdfStream);
I used to think it was because objectsContext
could not create other objects before PDFStream
was created.
as it says in the wiki
Important – Streams MUST always be written inside an indirect object definition. So make sure to call StartNewIndirectObject (or when modifying, StartModifiedIndirectObject), before starting the stream, and make sure you are clear of any object writing at the time.
In fact, I misunderstood wiki.
Now I understand that the reason for my Segmentation fault
is that I repeatedly released the DictionaryContext
, but I didn't think of this at the time.
thanks!
from pdf-writer.
Related Issues (20)
- Can not modify a document by creating a new form XObject and using it in one of the pages HOT 3
- [Question] - pdf to image HOT 1
- Question about attachments HOT 2
- some example projects in wiki are missing 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
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.