Quartz 2D programming guide-PDF document creation, display and conversion

Source: Internet
Author: User

PDF files are stored in vector graphics, text, and bitmap dependent on resolutions and used in a series of instructions of programs. A PDF file can contain multiple pages of graphics and text. PDF can be used to create cross-platform, read-only documents, or to draw images dependent on resolutions.
Quartz creates high-fidelity PDF documents for all applications, which retain the rendering operations of the application, as shown in 13-1. The results of the PDF document will be optimized through other products of the system or third method. The PDF File Created by quartz is correctly displayed in both preview and acrobat.


Quartz not only uses PDF as its digital page, it also contains some APIs to display and generate PDF files, and complete some other PDF-related work.

Open and view PDF
Quartz provides the cgw.documentref data type to represent PDF documents. You can use cgw.documentcreatewithprovider or cgw.documentcreatewithurl to create a cgw.document object. After creating the cg1_document object, we can draw it into the graphic context. Figure 13-2 shows how to draw a PDF document in a form.

The code listing 13-1 shows how to create a cg1_document object and obtain the number of pages of the document.

Copy code
  1. Cg1_documentrefmyget1_documentref (const char * filename)
  2. {
  3. Cfstringref path;
  4. Cfurlref URL;
  5. Cgw.documentref document;
  6. Size_t count;
  8. Path = cfstringcreatewithcstring (null, filename, kcfstringencodingutf8 );
  10. Url = cfurlcreatewithfilesystempath (null, path, kcfurlposixpathstyle, 0); // 1 create a cfurl object
  12. Cfrelease (PATH );
  14. Document = cgw.documentcreatewithurl (URL); // 2 create a cf1_document object
  15. Cfrelease (URL );
  17. Count = cgw.documentgetnumberofpages (document); // 3. Obtain the document page number.
  18. If (COUNT = 0 ){
  19. Printf ("'% s' needs at least onepage! ", Filename );
  20. Return NULL;
  21. }
  23. Return document;
  24. }

The code list shows how to draw a pdf page to a graphic context.

Copy code
  1. Void mydisplaypdfpage (cgcontextrefmycontext, size_t pagenumber, const char * filename)
  2. {
  3. Cgw.documentref document;
  4. Cgpdfpageref page;
  6. Document = mygetaskdocumentref (filename); // 1 create an upload Document Object
  7. Page = cgw.documentgetpage (document, pagenumber); // 2. Obtain the PDF document on the specified page.
  8. Cgcontextdrawpdfpage (mycontext, page); // 3. Draw a PDF file to the context of the image.
  9. Cgw.documentrelease (document );
  10. }

Create a conversion for the pdf page
Quartz provides the function cgpdfpagegetdrawingtransform to create an affine transformation. This transformation is based on ing the box on the pdf page to the specified rectangle. The function prototype is:

Copy code
  1. Cgaffinetransformcgpdfpagegetdrawingtransform (
  2. Cgppageref page,
  3. Cgw.box box,
  4. Cgrect rect,
  5. Int rotate,
  6. Bool preserveaspectratio
  7. );

This function returns an affined transform using the following algorithm:
· Intersection the media, crop, bleed, trim, and art rectangles related to the PDF box type specified in the box parameter with the/mediabox entries on the specified pdf page. The intersection is a valid rectangle ).
· Set the rotation angle specified by the negative rectangle Rotation Parameter/rotate entry.
· Place the obtained rectangle in the middle specified by the rect parameter.
· If the rotate parameter is a non-zero multiple of 90, the function rotates the specified angle of the valid rectangel value. The positive value rotates to the right, and the negative value rotates to the left. Note that we pass in an angle instead of a radian. Remember that the/rotate entry on the pdf page also contains a rotation. The rotate parameter we provide is joined with the/rotate entry.
· If needed, you can scale the rectangle to make it consistent with the rectangle we provide.
· If we pass the true value to the preserveaspectradio parameter to specify the aspect ratio, the last rectangle will be consistent with the side of the rect parameter.

[Note: The translation above is not very good]

For example, we can use this function to create a PDF browser similar to figure 13-3. If we provide a rotate left/rotate right attribute, you can call cgpdfpagegetdrawingtransform to calculate an appropriate conversion based on the current form size and rotation settings.

The program list 13-3 shows how to create a pdf page, apply an affined transform, and then draw a PDF.

Copy code
  1. Listing 13-3 creating an affine transform for APDF page
  2. Void mydrawpdfpageinrect (cgcontextref context, cgpdfpageref page, cg?box box, cgrect rect, int rotation, bool preserveaspectratio)
  3. {
  4. Cgaffinetransform m;
  6. M = cgpdfpagegetdrawingtransform (page, box, rect, rotation, preserveaspectrato );
  8. Cgcontextsavegstate (context );
  10. Cgcontextconcatctm (context, M );
  12. Cgcontextcliptorect (context, cgpdfpagegetboxrect (page, box ));
  14. Cgcontextdrawpdfpage (context, page );
  16. Cgcontextrestoregstate (context );
  18. }

Create PDF File
It is easy to use quartz to create a PDF file and draw other image contexts. We specify a PDF file address, set a PDF image context, and use the same drawing program as other graphics contexts. The mycreatepdffile function shown in code listing 13-4 shows all the work of creating a PDF.
Note that the code is used to draw a PDF in cgw.contextbeginpage and cgw.contextendpage. We can pass a cfdictionary object to specify page attributes, including media, crop, bleed, trim, and art boxes.
Listing 13-4Creating a PDF file

Copy code
  1. Void mycreatepdffile (cgrectpagerect, const char * filename)
  2. {
  3. Cgcontextref implements context;
  4. Cfstringref path;
  5. Cfurlref URL;
  6. Cfdata boxdata = NULL;
  7. Cfmutabledictionaryref mydictionary = NULL;
  8. Cfmutabledictionaryref pagedictionary = NULL;
  10. Path = cfstringcreatewithcstring (null, filename, kcfstringencodingutf8 );
  12. Url = cfurlcreatewithfilesystempath (null, path, kcfurlposixpathstyle, 0 );
  14. Cfrelease (PATH );
  16. Mydictionary = cfdictionarycreatemutable (null, 0, & kcftypedictionarykeycallbacks, & kcftypedictionaryvaluecallbacks );
  18. Cfdictionarysetvalue (mydictionary, kcg1_contexttitle, cfstr ("mypdf file "));
  20. Cfdictionarysetvalue (mydictionary, kcg1_contextcreator, cfstr ("myname "));
  22. Encryption context = cgw.contextcreatewithurl (URL, & pagerect, mydictionary );
  24. Cfrelease (mydictionary );
  26. Cfrelease (URL );
  28. Pagedictionary = cfdictionarycreatemutable (null, 0, & kcftypedictionarykeycallbacks, & kcftypedictionaryvaluecallbacks );
  30. Boxdata = cfdatacreate (null, (const uint8 *) & pagerect, sizeof (cgrect ));
  32. Cfdictionarysetvalue (pagedictionary, kcg1_contextmediabox, boxdata );
  34. Cgw.contextbeginpage (encryption context, & pagerect );
  36. Mydrawcontent (encryption context );
  38. Cgw.contextendpage (encryption context );
  40. Cgcontextrelease (encryption context );
  42. Cfrelease (pagedictionary );
  44. Cfrelease (boxdata );
  45. }

Add Link
We can add links and anchor points in the PDF context. Quartz provides three functions, each of which takes the PDF image context as the parameter and has link information:
· Cgw.contextseturlforrect allows us to open a URL when clicking the rectangle in the current pdf page.
· Cgw.contextsetdestinationforrect specifies to set the target when you click the rectangle area on the current pdf page to jump. We need to provide a target name.
· Cgw.contextadddestinationatpoint specifies to set the target when you click a point on the current pdf page to jump. We need to provide a target name.

Protect PDF content
To protect PDF content, you can specify some security options in the secondary dictionary and pass them to cgw.contextcreate. You can set whether the owner password, user password, and PDF file can be printed or copied by using the following keywords:
· Kcgw.contextownerpassword: defines the owner password of the PDF document. If this value is specified, the document is encrypted using the owner password; otherwise, the document is not encrypted. The value of this keyword must be an ASCII cfstring object. Only the first 32 bits are used for passwords. This value has no default value. If the value cannot be expressed as ASCII, the document cannot be created and null is returned. Quartz uses 40-bit encryption.
· Kcg1_contextuserpassword: defines the user password of the PDF document. If the document is encrypted, the value is the document's user password. If not specified, the user password is blank. The value of this keyword must be an ASCII cfstring object. Only the first 32 bits are used for passwords. If the value cannot be expressed as ASCII, the document cannot be created and null is returned.
· Kcg?contextallowsprinting: Specifies whether the document can be printed when the user password lock is used. The value must be a cfboolean object. The default value is kcgbooleantrue.
· Kcg?contextallowscopying: Specifies whether the document can be copied when the user password lock is used. The value must be a cfboolean object. The default value is kcgbooleantrue.
Code list 14-4 (next chapter) shows how to check whether the PDF document is locked and use a password to open the document.

Http://www.cocoachina.com/bbs/read.php? Tid = 83761

Quartz 2D programming guide-PDF document creation, display and conversion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.