-
Notifications
You must be signed in to change notification settings - Fork 217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add watermark to PDF #211
Comments
Hi :) To write Oblique text all you have to do is find a font that's oblique. Same as you would for writing text in bold or italic. For example, here is a courier oblique version - https://fontsgeek.com/fonts/Courier-Oblique. As for opacity, there are a few discussions in HummusJS (the JS version of this lib) and i think the most fitting is this one. While it discusses regular paths and not text, the solution is the same. Please have a look, it might be a shorter version of a longer explanation following here. If you want help with converting to C++ do let me know and i'll help. The point is to apply a graphic static prior to drawing the said text (or path) that includes an opacity factor in it.
here's an example of how to create the graphic state dict: ObjectsContext *objCxt = pdfWriter->GetObjectsContext();
ObjectIDType gsId = objCxt->StartNewIndirectObject();
DictionaryContext* dict = objCxt->StartDictionary()
dict->WriteKey("type");
dict->WriteNameValue("ExtGState");
dict->WriteKey("ca");
objCxt->WriteNumber(0.5);
objCxt->EndLine();
objCxt->EndDictionary(dict);
here's an example of referring to the dictionary from a page resources dictionary: string gstateName = myPage->GetResourcesDictionary().AddExtGStateMapping(gsId);
For example:
That should be it. You can use the more low level methods to write the text, or alternatively draw a path...doesn't matter as long as you call the cxt->gs(myPage->GetResourcesDictionary().AddExtGStateMapping(gsId)); p.s. there's an example here (and links to code) that shows how to create a single watermark in a form and then reuse that form in multiple pages. again in JS, but can be translated to the C++ operators by mostly moving from camelcasing to titlecasing. |
Thanks for replying Here is my code now .. pdfWriter.StartPDF(input, ePDFVersion13); But opacity not changing |
Hi, To directly deal with errors in the example:
PDFVersion should probably also change to 14 and not stay in 13, as things like "ca" key are only supported from 14...but that's not a biggy, i think acrobat ignores it. So the code should be: pdfWriter.StartPDF(input, ePDFVersion14);
ObjectsContext& objCtx = pdfWriter.GetObjectsContext();
ObjectIDType gsID = objCtx.StartNewIndirectObject();
DictionaryContext* dict = objCtx.StartDictionary();
dict->WriteKey("type");
dict->WriteNameValue("ExtGState");
dict->WriteKey("ca");
objCtx.WriteDouble(0.5);
objCtx.EndLine();
objCtx.EndDictionary(dict);
objCtx.EndIndirectObject();
auto* page = new PDFPage();
page->SetMediaBox(PDFRectangle(0,0,595,842));
std::string gsName = page->GetResourcesDictionary().AddExtGStateMapping(gsID);
PageContentContext* pageContentContext = pdfWriter.StartPageContentContext(page);
PDFUsedFont* font = pdfWriter.GetFontForFile(font_path);
pageContentContext->q();
pageContentContext->gs(gsName);
pageContentContext->BT();
pageContentContext->k(0, 100, 100, 0);
pageContentContext->Tf(font, font_size);
pageContentContext->Tm(1,0,0,1,100,100);
pageContentContext->Tj("Test Text");
pageContentContext->ET();
pageContentContext->Q();
pdfWriter.EndPageContentContext(pageContentContext);
pdfWriter.WritePageAndRelease(page);
pdfWriter.EndPDF(); I added a test called WatermarkTest now to serve as an example. |
Worked like a charm |
How to use it in modified page ? Not new empty page std::string gsName = page->GetResourcesDictionary().AddExtGStateMapping(gsID); |
The rest is the same. |
Hi, See example here. |
My question is .. How to add oblique text with custom opacity (watermark)
The text was updated successfully, but these errors were encountered: