PDF for .NET - Migrating from ComponentOne PDF to GrapeCity Documents for PDF
If you are using ComponentOne PDF (C1Pdf) to generate PDFs in .NET, you should migrate to our new, high-performance API - GrapeCity Documents for PDF (GcPdf). GrapeCity Documents for PDF is a fast, feature-rich PDF API for .NET Standard 2.0. You can generate documents with speed, and memory efficiency, without any dependencies on Acrobat.
Both ComponentOne PDF and GrapeCity Documents for PDF include many PDF functions. With GcPDF you can do more!
Explore the advanced features of GrapeCity Documents for PDF API.
GrapeCity Documents for PDF additional features:
- Advanced text handling with full fonts and multi-language support
- Advanced paragraph handling and formatting
- Add, modify and delete content from existing PDFs
- Additional annotations
- Text and image stamps
- Generate linearized PDFs
- Update documents incrementally
Our complete list of GcPdf features:
Features | GcPdf | C1PDF |
Platform Support: .NET Standard 2.0 | yes | no |
1. Supported PDF versions | ||
1.1 PDF/UA | yes (planned) | no |
2. Fonts | ||
2.1 WOFF Fonts | yes | no |
2.2 Ligatures and other font features | yes | no |
3. Text | ||
3.1 Text properties | ||
3.3.1 Overline | yes | no |
3.3.2 Word spacing | yes | no |
3.3.3 Line spacing | yes | no |
3.2 Add single, multiline, and multipage text | yes | yes (partial) |
3.3 Add multilingual text using unicode fonts. | yes | no |
3.4 Advanced text splitting | yes | no |
3.5 Widow and orphan control | yes | no |
3.6 Keep with next | no | |
3.7 Keep together | no | |
3.8 Embedded, inline and anchored objects in text | yes | no |
3.9 Vertical direction (clockwise, counter clockwise) | yes | no |
3.10 First line indent | yes | no |
3.11 Left hanging paragraph | yes | no |
3.12 Full justification | yes | no |
3.13 Add paragraphs | yes | no |
3.14 Paragraph formatting | yes | no |
3.15 Extract text from pages. | yes (planned) | no |
3.16 Search text from pages | yes | no |
3.17 Get text segments | yes | no |
3.18 Support for special characters | ||
3.18.1 Ruby characters | yes | no |
3.18.2 Automatic character shaping for Arabic/Asian languages (Hindi) | yes | yes (Partial) |
4. Images | ||
4.1 Extract images. | yes (planned) | no |
5. Document | ||
5.1 Modify existing document | yes | no |
5.2 Open existing PDF documents from local or storage file | yes | no |
5.3 Open existing PDF documents from stream | yes | no |
5.4 Set page display properties | yes | no |
5.5 Linearization (optimization for the web) | yes | no |
5.6 Get document viewer properties | yes | no |
5.7 Get page display properties | yes | no |
5.8 Get PDF file information | yes | no |
5.9 Custom metadata | yes | no |
5.10 Set XMP metadata | yes | no |
5.11 Get XMP metadata | yes | no |
5.12 Incremental save | yes | no |
5.13 Page properties | ||
5.13.1 Cropbox | yes | no |
5.13.2 Bleedbox | yes | no |
5.13.3 Artibox | yes | no |
5.13.4 Trimbox | yes | no |
5.13.5 Page transition | yes | no |
5.13.6 Zoom factor | yes | no |
5.14 Multiple columns | yes | no |
6. Bookmarks | ||
6.1 Delete bookmarks | yes | no |
6.2 Get bookmarks | yes | no |
6.3 Get child bookmarks | yes | no |
6.4 Modify bookmarks | yes | no |
6.5 Modify child bookmarks | yes | no |
7. Annotations | ||
7.1 Get annotations | yes | no |
7.2 Delete annotations | yes | no |
7.3 Modify annotations | yes | no |
8. Annotation Types | ||
8.1 Ink Annotation | yes | no |
8.2 FreeText annotation | yes | no |
8.3 Polygon annotation | yes | no |
8.4 Pop-up annotation | yes | no |
8.5 Rubber stamp annotation | yes | no |
9. Forms | ||
9.1 Delete fields | yes | no |
9.2 Modify fields | yes | no |
9.3 Get field values | yes | no |
9.4 Move field to new location | yes | no |
9.5 Flatten fields (a particular field or all) | yes | no |
9.6 Add and delete list item | yes | no |
9.7 Set button script | yes | no |
10. Stamp and watermark | ||
10.1 Add text stamp | yes | no |
10.2 Add image stamp | yes | no |
11. Security and signatures | ||
12. Decrypt PDF | yes | no |
13. Links and Actions | ||
13.1 Create JavaScript link | yes | no |
13.2 Remove document open action | yes | no |
13.3 Extract links | yes | no |
14. Attachments | ||
14.1 Get attachments | yes | no |
14.2 Delete attachments | yes | no |
14.3 Get attachment information | yes | no |
15. Page | ||
15.1 Get page count | yes | no |
15.2 Concatenate PDF files | yes | no |
15.3 Delete PDF pages. | yes | no |
16. Graphic objects | yes | |
16.1 Apply patterns and shadings to the graphics | yes | no |
Migrating C1Pdf project to GcPdf
Suppose you have a project that uses C1Pdf to generate a PDF article with multiline text and paragraphs.
//Create C1PdfDocument
C1PdfDocument doc = new C1PdfDocument();
//Set Paragraph properties
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Near;
//Define Font properties to display the title
System.Drawing.Font f = new System.Drawing.Font("Arial", 16, System.Drawing.FontStyle.Regular);
//Draw Title
doc.DrawString("Introduction", f, Brushes.MediumBlue, new System.Drawing.PointF(230, 10), stringFormat);
//Re-define new Font properties for sub-title
f = new System.Drawing.Font("Arial", 12, System.Drawing.FontStyle.Regular);
//Draw sub-title
doc.DrawString("The Importance of Wetlands", f, Brushes.MediumBlue, new System.Drawing.PointF(200, 40), stringFormat);
//Re-define font object for paragraph text
f = new System.Drawing.Font("Arial", 9, System.Drawing.FontStyle.Regular);
//Draw paragraph
int k = 0;
using (StreamReader file = new StreamReader(@"..\..\temp.txt"))
{
string line;
while ((line = file.ReadLine()) != null)
{
doc.DrawString(line, f, Brushes.Black, new System.Drawing.PointF(50, 80 + k), stringFormat);
k += 20;
}
}
doc.Save("C1Pdf_Doc.pdf");
System.Diagnostics.Process.Start("C1Pdf_Doc.pdf");
You can migrate this project to GcPdf, using following steps:
Step 1. Remove C1.C1Pdf.4 dll from Solution Explorer
Step 2: Add 'GrapeCity.Documents.Pdf' nuget package from nuget.org
Step 3: Remove namespace -'using C1.C1Pdf' add namespaces for GcPdf
using GrapeCity.Documents.Pdf
using Grapecity.Documents.Text
Step 4: Modify the Pdf document object
Modify Pdf document object from - C1PdfDocument doc=new C1PdfDocument();
to
"GcPdfDocument doc=new GcPdfDocument();"
Step 5: Add new page to the document and point the GcPdfGraphics to the page, to draw content over the page.
var page=doc.NewPage();
GcPdfGraphics g = page.Graphics;
Step 6: Remove font object
Remove this code:
//Define Font properties to display the title
System.Drawing.Font f = new System.Drawing.Font("Arial", 16, System.Drawing.FontStyle.Regular);
Step 7: Define TextLayout class that adds text to PDF file
The TextLayout class represents one or more paragraphs of text and can be drawn directly to the page Graphics. With GcPdf, you can additionally set the height, width of the text layout, along with the margins.
Define the TextLayout class and associate the FontCollection with it.
var tl = new TextLayout()
{
MaxWidth = doc.PageSize.Width,
MaxHeight = doc.PageSize.Height,
MarginLeft = 72,
MarginRight = 72,
MarginTop = 72,
MarginBottom = 72,
};
Step 8: Draw titles on the page
Remove following lines:
//Set Paragraph properties
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Near;
//Define Font properties to display the title
System.Drawing.Font f = new System.Drawing.Font("Arial", 16, System.Drawing.FontStyle.Regular);
//Draw Title
doc.DrawString("Introduction", f, Brushes.MediumBlue, new System.Drawing.PointF(230, 10), stringFormat);
//Re-define new Font properties for sub-title
f = new System.Drawing.Font("Arial", 12, System.Drawing.FontStyle.Regular);
//Draw sub-title
doc.DrawString("The Importance of Wetlands", f, Brushes.MediumBlue, new System.Drawing.PointF(200, 40), stringFormat);
You can align the titles at the center of the page and draw the titles on the page with GcPdf.
Note: The TextFormat class can specify the font properties you want to use on the titles or any text in PDF File. It allows individual formatting of text fragments using different fonts, font styles and colors.
Use the TextLayout class to set the alignment of the text and add the titles to it. Then define the TextFormat class to hold the Font properties for your titles. Finally, draw the titles on the page.
tl.TextAlignment = TextAlignment.Center;
var colorBlue = Color.FromArgb(0x3B, 0x5C, 0xAA);
tl.Append("Introduction\n", new TextFormat() { FontSize = 16, ForeColor = colorBlue, FontName = "Arial", FontStyle= GrapeCity.Documents.Text.FontStyle.Regular });
tl.Append("The Importance of Wetlands", new TextFormat() { FontSize = 12, ForeColor = colorBlue });
tl.PerformLayout(true);
g.DrawTextLayout(tl, PointF.Empty);
Step 9: Draw paragraphs, add additional paragraph formatting with GcPdf
Remove the code to draw paragraphs:
int k = 0;
using (StreamReader file = new StreamReader(file))
{
string line;
while ((line = file.ReadLine()) != null)
{
doc.DrawString(line, f, Brushes.Black, new System.Drawing.PointF(50, 80 + k), stringFormat);
k += 20;
}
}
Add additional paragraph formatting properties with GcPdf.
- FirstLineIndent
- TextAlignment
- Uniform line spacing
- Paragraph Spacing
Use the TextLayout class defined before to set the above properties:
tl.FirstLineIndent = 36;
tl.TextAlignment = TextAlignment.Justified;
tl.ParagraphSpacing = 12;
tl.UniformLineSpacing = 15;
Add paragraphs
var tf = new TextFormat() { FontSize = 9, ForeColor = Color.Black, FontName = "Arial", FontStyle = GrapeCity.Documents.Text.FontStyle.Regular };
using (StreamReader file = new StreamReader(@"..\..\temp.txt"))
{
string line;
while ((line = file.ReadLine()) != null)
{
if (string.IsNullOrWhiteSpace(line))
tl.AppendLine(tf);
else
tl.Append(line + " ", tf);
}
}
Draw the paragraphs
tl.PerformLayout(true);
g.DrawTextLayout(tl, PointF.Empty);
Step 10: Save to PDF
doc.Save("GcPdf_Doc.pdf");
System.Diagnostics.Process.Start("GcPdf_Doc.pdf");
The complete code should look like this:
//Define GcPdfDocument object
GcPdfDocument doc = new GcPdfDocument();
//Add new page and point graphics to it to draw content on the page
var page = doc.NewPage();
GcPdfGraphics g = page.Graphics;
//Define Title forecolor
var colorBlue = Color.FromArgb(0x3B, 0x5C, 0xAA);
//Define TextLayout class to add text to PDF File. Associate FontCollection with it.
TextLayout tl = new TextLayout()
{
MaxWidth = doc.PageSize.Width,
MaxHeight = doc.PageSize.Height,
MarginLeft = 72,
MarginRight = 72,
MarginTop = 72,
MarginBottom = 72,
};
//Align Titles to center
tl.TextAlignment = TextAlignment.Center;
//Add Title and Sub-title to TextLayout
//Define TextFormat class to hold the Font properties you want to set on the Titles
tl.Append("Introduction\n", new TextFormat() { FontSize = 16, ForeColor = colorBlue, FontName = "Arial", FontStyle = GrapeCity.Documents.Text.FontStyle.Regular });
tl.Append("The Importance of Wetlands", new TextFormat() { FontSize = 12, ForeColor = colorBlue, FontName = "Arial", FontStyle = GrapeCity.Documents.Text.FontStyle.Regular });
//Draw the titles on the page graphics
tl.PerformLayout(true);
g.DrawTextLayout(tl, PointF.Empty);
//Clear TextLayout
tl.Clear();
//Define additional Paragraph formatting properties
tl.MarginTop = tl.ContentHeight + 72*2;
tl.TextAlignment = TextAlignment.Justified;
tl.ParagraphSpacing = 12;
tl.FirstLineIndent = 36;
tl.UniformLineSpacing = 15;
//Define TextFormat to hold Font properties you want to apply on the paragraphs
var tf = new TextFormat() { FontSize = 9, ForeColor = Color.Black, FontName = "Arial", FontStyle = GrapeCity.Documents.Text.FontStyle.Regular };
//Read the txt file line by line and add the lines to the TextLayout.
using (StreamReader file = new StreamReader(@"..\..\temp.txt"))
{
string line;
while ((line = file.ReadLine()) != null)
{
if (string.IsNullOrWhiteSpace(line))
tl.AppendLine(tf);
else
tl.Append(line + " ", tf);
}
}
//Draw the paragraphs
tl.PerformLayout(true);
g.DrawTextLayout(tl, PointF.Empty);
//Save the pdf
doc.Save("GcPdf_Doc.pdf");
System.Diagnostics.Process.Start("GcPdf_Doc.pdf");
Congratulations! You have migrated your project to GcPdf. Below are two generated PDFs from each of the controls. Notice the advanced text and paragraph formatting in PDF generated with GcPdf.