I need to embed some custom meta data to a PDF file using the Apple's Objective C SDK. Is there any working sample code available that does that?
What I need is very basic. Open an existing pdf and add some meta data and then save it.
Example of information stored will be like.... a customer's name, contact number, items ordered.
I saw that this "CGPDFContextAddDocumentMetadata" supports metadata but I don't know how the meta data look like and how is it used passed to this function.
Any help will be greatly appreciated...:)
Metadata is metadata (of course, it is recommended by Adobe that you use XMP XML). So long as you create a valid CFDataRef and pass it into arg two, you're pretty much good to go with anything. For example, here's how to pass the string "Hello World" into a PDF's metadata:
void MakeAPDF()
{
CGRect mediaRect = CGRectMake(0, 0, 400, 600);
// use your own rect instead
CFMutableDataRef result = CFDataCreateMutable(kCFAllocatorDefault, 0);
CGDataConsumerRef PDFDataConsumer = CGDataConsumerCreateWithCFData(result);
// mark the PDF as coming from your program
CFMutableDictionaryRef auxInfo = CFDictionaryCreateMutable(kCFAllocatorDefault, 1, NULL, NULL);
CFDictionaryAddValue(auxInfo, kCGPDFContextCreator, CFSTR("Your Programs Name"));
CFDictionaryRef auxillaryInformation = CFDictionaryCreateCopy(kCFAllocatorDefault, auxInfo);
CFRelease(auxInfo);
// create a context to draw into
CGContextRef graphicContext = CGPDFContextCreate(PDFDataConsumer, &mediaRect, auxillaryInformation);
CFRelease(auxillaryInformation);
CGDataConsumerRelease(PDFDataConsumer);
// actually make the call to embed your String
NSString* str= #"Hello World";
NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding];
CFDataRef cfdata = CFDataCreate(NULL, [data bytes], [data length]);
CGPDFContextAddDocumentMetadata(graphicContext, cfdata);
CGContextBeginPage(graphicContext, &mediaRect);
// do your drawing, like this grey rectangle
CGContextSetGrayFillColor(graphicContext, 0.5, 0.5);
CGContextAddRect(graphicContext, mediaRect);
CGContextFillPath(graphicContext);
// end your drawing
CGContextEndPage(graphicContext);
CGContextFlush(graphicContext);
CGPDFContextClose(graphicContext);
}
An XMP file might look like this:
<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 4.2.1-c041">
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:format>application/pdf</dc:format>
<dc:title>
<rdf:Alt>
<rdf:li />
</rdf:Alt>
</dc:title>
<dc:description>
<rdf:Alt>
<rdf:li />
</rdf:Alt>
</dc:description>
<dc:creator>
<rdf:Seq>
<rdf:li />
</rdf:Seq>
</dc:creator>
</rdf:Description>
<rdf:Description rdf:about="" xmlns:pdf="http://ns.adobe.com/pdf/1.3/">
<pdf:Keywords />
<pdf:Producer />
</rdf:Description>
<rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/">
<xmp:CreatorTool />
<xmp:CreateDate>2011-02-10T11:41:05+02:00</xmp:CreateDate>
<xmp:ModifyDate>2011-02-10T11:41:06+02:00</xmp:ModifyDate>
</rdf:Description></rdf:RDF>
</x:xmpmeta>
<?xpacket end="w"?>
UIKit has some methods that make this simpler:
// use your own rect instead
CGRect pageRect = CGRectMake(0, 0, 400, 600);
// mark the PDF as coming from your program
NSDictionary *auxillaryInformation = #{(NSString *)kCGPDFContextCreator: #"Koedal, Inc."};
UIGraphicsBeginPDFContextToFile([[self URLOfPDF] path], pageRect, auxillaryInformation);
// Embed metadata into PDF, I'm pulling it from a textView
NSString *PDFMetadata = self.metadataTextView.text;
NSData *data = [PDFMetadata dataUsingEncoding:NSUTF8StringEncoding];
CGContextRef PDFContext = UIGraphicsGetCurrentContext();
CGPDFContextAddDocumentMetadata(PDFContext, (CFDataRef)data);
NSDictionary *textAttributes = #{ NSFontAttributeName : [UIFont boldSystemFontOfSize:20],
NSForegroundColorAttributeName : [UIColor blackColor]
};
// do your drawing, like drawing this string
UIGraphicsBeginPDFPage();
NSString *content = self.contentTextField.text;
[content drawAtPoint:CGPointMake(150, 280) withAttributes:textAttributes];
// end your drawing
UIGraphicsEndPDFContext();
Related
I am using a open source UILabel subclass STTweetLabel v2.22 (Github) and trying to show emoji in the label. During my test it seems that the code can handle most cases correctly but sometimes I see this:
Just wondering why this could happen, and what could be a possible fix I should look into..
Thanks!
-- Update (adding code used to decode strings from server) --
NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
NSString *decoded = [[NSString alloc] initWithData:data encoding:NSNonLossyASCIIStringEncoding];
Hi I have got one solution for that library you can find height with emoji.
Please use <CoreText/CoreText.h> framework and use below code.
- (CGFloat)heightStringWithEmojis:(NSString*)str fontType:(UIFont *)uiFont ForWidth:(CGFloat)width {
// Get text
CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
CFAttributedStringReplaceString (attrString, CFRangeMake(0, 0), (CFStringRef) str );
CFIndex stringLength = CFStringGetLength((CFStringRef) attrString);
// Change font
CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef) uiFont.fontName, uiFont.pointSize, NULL);
CFAttributedStringSetAttribute(attrString, CFRangeMake(0, stringLength), kCTFontAttributeName, ctFont);
// Calc the size
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
CFRange fitRange;
CGSize frameSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, 0), NULL, CGSizeMake(width, CGFLOAT_MAX), &fitRange);
CFRelease(ctFont);
CFRelease(framesetter);
CFRelease(attrString);
return frameSize.height +4;
}
Let me know thoughts....!!!
Several things:
Do not use encoding:NSNonLossyASCIIStringEncoding, emoji are not ASCII. Use NSUTF8StringEncoding.
Why are you converting to NSData and then back to an NSString? That makes no sense.
There is something else going on here.
I want to add the signature on existing pdf. I have done this in following way:
1) Load existing pdf on UIView say mainView.
2) Add a signature image on mainView.
3) Call a following function
-(NSMutableData *)getPDFDatafromUIView
{
DebugLog(#"");
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [NSMutableData data];
// Points the pdf converter to the mutable data object and to the UIView to be converted
UIGraphicsBeginPDFContextToData(pdfData, mainView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
mainView.layer renderInContext:pdfContext];
// remove PDF rendering context
UIGraphicsEndPDFContext();
return pdfData;
}
4) This function block your UI for a while, so call it on new thread
[NSThread detachNewThreadSelector:#selector(launchExportViewForExportDrawing) toTarget:self withObject:nil];
Using above method I get a pdf data with signature which contain the old pdf data too.
But for above method I must need to show the pdf in UIView. If I want do the above thing without loading on UIView, without showing pdf to user, How do I do that?
I am able to add a Image on pdf with creating the new pdf page. How do I add a image on existing pdf?
I have solved by creating the new PDF and drawing the pdf data and signature on it. Please refer following code:
// For adding the Siganture we need to wite the content on new PDF
-(void) addSignature:(UIImage *) imgSignature onPDFData:(NSData *)pdfData {
NSMutableData* outputPDFData = [[NSMutableData alloc] init];
CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData((CFMutableDataRef)outputPDFData);
CFMutableDictionaryRef attrDictionary = NULL;
attrDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(attrDictionary, kCGPDFContextTitle, CFSTR("My Doc"));
CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, NULL, attrDictionary);
CFRelease(dataConsumer);
CFRelease(attrDictionary);
CGRect pageRect;
// Draw the old "pdfData" on pdfContext
CFDataRef myPDFData = (__bridge CFDataRef) pdfData;
CGDataProviderRef provider = CGDataProviderCreateWithCFData(myPDFData);
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(provider);
CGDataProviderRelease(provider);
CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1);
pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
CGContextBeginPage(pdfContext, &pageRect);
CGContextDrawPDFPage(pdfContext, page);
// Draw the signature on pdfContext
pageRect = CGRectMake(0, 0,imgSignature.size.width , imgSignature.size.height);
CGImageRef pageImage = [imgSignature CGImage];
CGContextDrawImage(pdfContext, pageRect, pageImage);
// release the allocated memory
CGPDFContextEndPage(pdfContext);
CGPDFContextClose(pdfContext);
CGContextRelease(pdfContext);
// write new PDFData in "outPutPDF.pdf" file in document directory
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pdfFilePath =[NSString stringWithFormat:#"%#/outPutPDF.pdf",docsDirectory];
[outputPDFData writeToFile:pdfFilePath atomically:YES];
}
Rohit Answer is working perfectly,But it works only single page so i just Added some code for all pages displaying with image(image shown in single page reaming pages are looks same).Thanks #Rohit
NSString *path = [[NSBundle mainBundle] pathForResource:#"PartB" ofType:#"pdf"];
NSURL *docURL = [NSURL fileURLWithPath:path];
NSString *pdfName = [NSString stringWithFormat:#"%#",docURL];
NSLog(#"pdfName: %#", pdfName);
NSData *pdfData = [NSData dataWithContentsOfFile:path];
NSURL *pdfURL = [[NSBundle mainBundle] URLForResource:#"PartB.pdf" withExtension:nil];
CGPDFDocumentRef pdf1 = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
long pageCount = CGPDFDocumentGetNumberOfPages(pdf1);
NSLog(#"the page count %ld",pageCount);
NSMutableData* outputPDFData = [[NSMutableData alloc] init];
CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData((CFMutableDataRef)outputPDFData);
CFMutableDictionaryRef attrDictionary = NULL;
attrDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(attrDictionary, kCGPDFContextTitle, CFSTR("My Doc"));
CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, NULL, attrDictionary);
CFRelease(dataConsumer);
CFRelease(attrDictionary);
CGRect pageRect;
// Draw the old "pdfData" on pdfContext
CFDataRef myPDFData = (__bridge CFDataRef) pdfData;
CGDataProviderRef provider = CGDataProviderCreateWithCFData(myPDFData);
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(provider);
CGDataProviderRelease(provider);
for (int k=1; k<=pageCount; k++) {
CGPDFPageRef page3 = CGPDFDocumentGetPage(pdf, k);
pageRect = CGPDFPageGetBoxRect(page3, kCGPDFMediaBox);
CGContextBeginPage(pdfContext, &pageRect);
CGContextDrawPDFPage(pdfContext, page3);
if (k==pageSelect) {
pageRect = CGRectMake(0, 0,100 , 100);
CGImageRef pageImage = [mainImage.image CGImage];
CGContextDrawImage(pdfContext, pageRect, pageImage);
}
CGPDFContextEndPage(pdfContext);
}
CGPDFContextClose(pdfContext);
CGContextRelease(pdfContext);
// write new PDFData in "outPutPDF.pdf" file in document directory
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pdfFilePath =[NSString stringWithFormat:#"%#/outPutPDF.pdf",docsDirectory];
[outputPDFData writeToFile:pdfFilePath atomically:YES];
NSLog(#"save the pdf %#",pdfFilePath);
I'm not sure I understand your problem completely. From what I can tell you have a UIView that you're rendering onto a PDF document, and you want to add a signature image and not show it to the user.
If you're trying to add a "signature image" to your pdf, you can add a UIImageView to the UIView that you're rendering to the pdf.
If you don't want the user to see the UIView in the mean time, you could change it's origin to somewhere off bounds, for example:
CGRect originalFrame = mainView.frame;
CGRect newFrame = mainView.frame;
newFrame.origin.x = -newFrame.size.width;
newFrame.origin.y = -newFrame.size.height;
mainView.frame = newFrame;
//do all your PDF stuff here
mainView.frame = originalFrame;
I hope that helps. If it doesn't, please clarify the problem :)
I have a NSMutableData which contains pdf data. I want to delete certain pages from the pdf and return the pdf. I have tried creating the CGPDFContext, CGPDFPageRef, etc, but nothing really worked out. I have the below code, Can someone help me to fill the 3 steps.
-(NSMutableData *)deletePagesofPDF:(NSMutableData *)data withStartPage:(int)startpage withEndPage:(int)endpage{
NSData *data1 = data;
CFDataRef myPDFData = (__bridge CFDataRef)data1;
CGDataProviderRef provider = CGDataProviderCreateWithCFData(myPDFData);
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(provider);
//step 1 delete pages from startpage to endpage.
//step 2 save pdf
//step 3 convert pdf back to NSMutabledata
return data;
}
Edit 1
I have decided to change my strategy. Instead of deleting the pages, i am now creating a new pdf with only the pages i require from another PDF. Please see the code below, somehow it crashes while creating the pdf context. error is : address doesn't contain a section that points to a section in a object file. I could not go past creating the context. Please correct me if there is something wrong after creating the context.
-(void)saveNSDataToPDFFile:(NSMutableData *)data{
NSData *data1 = data;
CFDataRef myPDFData = (__bridge CFDataRef)data1;
CGDataProviderRef provider = CGDataProviderCreateWithCFData(myPDFData);
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(provider);
CGPDFPageRef newPage = CGPDFDocumentGetPage(pdf, 2);
NSMutableData *pdfData = [[NSMutableData alloc] init];
CGRect mediaBox = CGRectMake(0, 0, 850, 1100);
CGContextRef pdfContext = CGPDFContextCreate((__bridge CGDataConsumerRef)(pdfData),&mediaBox,NULL);
CGPDFContextBeginPage (pdfContext,nil);
CGContextDrawPDFPage(pdfContext, newPage);
CGPDFContextClose(pdfContext);
[pdfData writeToFile:[self getDBPathPDf:#"Rajashekar.pdf"] atomically:YES];
}
EDIT 2
Sorry about my long post.
I can save the pdf file now without crashing but i cannot see the data inside the pdf. It just shows a medium sized box with "PDF" written inside it. Where am i going wrong?
-(void)saveNSDataToPDFFile:(NSMutableData *)data{
NSData *data1 = data;
CFDataRef myPDFData = (__bridge CFDataRef)data1;
CGDataProviderRef provider = CGDataProviderCreateWithCFData(myPDFData);
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(provider);
CGPDFPageRef newPage = CGPDFDocumentGetPage(pdf, 2);
NSMutableData *pdfData = [[NSMutableData alloc] init];
CGRect mediaBox = CGRectMake(0, 0, 850, 1100);
CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData((__bridge CFMutableDataRef)pdfData);
CGContextRef pdfContext = CGPDFContextCreate(consumer,&mediaBox,NULL);
if (pdfContext)
{
CGPDFContextBeginPage (pdfContext,nil);
CGContextDrawPDFPage(pdfContext, newPage);
CGPDFContextClose(pdfContext);
[pdfData writeToFile:[self getDBPathPDf:#"Rajashekar.pdf"] atomically:YES];
}
}
EDIT 3
Its working....:) i forgot to CGPDFContextEndPage(pdfContext); before i closed the context.
I am attempting to convert some text files into a pdf. Now the easiest thing in the world right now is to simply combine all of the text from the files into a single string and then use the iOS documentation here to render it into a single pdf. The trouble is, these are large text files; together they can equal well over 90 pages. Therefore I will need to add in some hyperlinks so I can create a table of contents at the top and the user can quickly move to the beginning of each text file rather than have to scroll through 60 pages to get to where they want to go.
Problem is, if I combine the txt files into a single string, I have no way of knowing when each file will end while it is paginating, therefore I wanted to add the files separately to the pdf before finally publishing it. Problem is, at best only the last txt file will show up rendered, most likely because it is overwriting the previous ones. Below is my code, any ideas?
- (void)savePDFFile:(NSString *)file_Name
{
// NSArray *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *homeDir = NSHomeDirectory();
NSString *saveDirectory = [NSString stringWithFormat: #"%#/%#", homeDir, #"Documents/"];
NSArray *fileAr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:saveDirectory error:nil];
// NSString *text = #"";
// NSMutableArray *textArray = [[NSMutableArray alloc] init];
NSInteger currentPage = 0;
NSString *completeString = #"";
for (NSString *string in fileAr) {
if([string hasSuffix:#"txt"]){
NSString *file = [NSString stringWithFormat: #"%#/%#", saveDirectory, string];
NSString *text =[NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];
completeString = [NSString stringWithFormat:#"%#%#", completeString, text];
}
}
for (NSString *string in fileAr) {
if([string hasSuffix:#"txt"]){
NSString *file = [NSString stringWithFormat: #"%#/%#", saveDirectory, string];
NSString *text =[NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];
// Prepare the text using a Core Text Framesetter
CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, (CFStringRef)text, NULL);
if (currentText) {
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)currentText);
// CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);
if (framesetter) {
NSString* pdfFileName = file_Name;
// Create the PDF context using the default page size of 612 x 792.
UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);
CFRange currentRange = CFRangeMake(0, 0);
BOOL done = NO;
do {
// Mark the beginning of a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);
// Draw a page number at the bottom of each page
currentPage++;
[self drawPageNumber:currentPage];
// Render the current page and update the current range to
// point to the beginning of the next page.
currentRange = [self renderPage:currentPage withTextRange:currentRange andFramesetter:framesetter];
// If we're at the end of the text, exit the loop.
if (currentRange.location == CFAttributedStringGetLength((CFAttributedStringRef)currentText))
done = YES;
} while (!done);
// Release the framewetter.
CFRelease(framesetter);
CFRelease(currentText);
}
}
// Close the PDF context and write the contents out.
UIGraphicsEndPDFContext();
} else {
NSLog(#"Could not create the framesetter needed to lay out the atrributed string.");
}
// Release the attributed string.
}
}
// Use Core Text to draw the text in a frame on the page.
- (CFRange)renderPage:(NSInteger)pageNum withTextRange:(CFRange)currentRange
andFramesetter:(CTFramesetterRef)framesetter
{
// Get the graphics context.
CGContextRef currentContext = UIGraphicsGetCurrentContext();
// Put the text matrix into a known state. This ensures
// that no old scaling factors are left in place.
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
// Create a path object to enclose the text. Use 72 point
// margins all around the text.
CGRect frameRect = CGRectMake(72, 72, 468, 648);
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);
// Get the frame that will do the rendering.
// The currentRange variable specifies only the starting point. The framesetter
// lays out as much text as will fit into the frame.
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
CGPathRelease(framePath);
// Core Text draws from the bottom-left corner up, so flip
// the current transform prior to drawing.
CGContextTranslateCTM(currentContext, 0, 792);
CGContextScaleCTM(currentContext, 1.0, -1.0);
// Draw the frame.
CTFrameDraw(frameRef, currentContext);
// Update the current range based on what was drawn.
currentRange = CTFrameGetVisibleStringRange(frameRef);
currentRange.location += currentRange.length;
currentRange.length = 0;
CFRelease(frameRef);
return currentRange;
}
I made progress and ran into another problem, so I asked an additional question here.
Eventually, with my own testing and a little bit of help, I solved the issue.
I have a method that returns NSData from a CGPathRef like so ...
+ (NSData *) createPDFDataWithCGPath: (CGPathRef) path mediaBox: (CGRect) mediaBox
{
CFMutableDataRef data = NULL;
if (path) {
CFAllocatorRef allocator = NULL;
data = CFDataCreateMutable(allocator, 0);
CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData(data);
CGContextRef context = CGPDFContextCreate(consumer, &mediaBox, NULL);
CFTypeRef keys[1] = { kCGPDFContextMediaBox };
CFTypeRef values[1] = { CFDataCreate(allocator, (const UInt8 *)&mediaBox, sizeof(CGRect)) };
CFDictionaryRef pageInfo = CFDictionaryCreate(allocator, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CGPDFContextBeginPage(context, pageInfo);
CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, 0, -mediaBox.size.height);
CGContextAddPath(context, path);
CGContextSetRGBFillColor(context, 0, 0, 0, 1);
CGContextFillPath(context);
CGPDFContextEndPage(context);
CFRelease(pageInfo);
CFRelease(values[0]);
CGPDFContextClose(context);
CGContextRelease(context);
CGDataConsumerRelease(consumer);
}
return (NSData *)data;
}
When I attempt to use this and write a PDF file from the data I get my file at the correct size but the paths are not drawn into the PDF ... it's an empty document basically.
Is it enough to just write the file like so ...
[maskData writeToFile: DOCUMENTS_PATH_WITH_STRING(#"maskData.pdf") atomically: YES];
or are their more hoops to jump though to write it as a PDF?
You're right.
If you have data in an NSData object then you can just write it on the disk. But you should add the correct extension (in your case .pdf).
It works for everything (videos, pictures, etc...)
This was a bit of a red herring. The paths were being drawn but had no strokes or fills. The code in my question above is sound and works as expected.