PdfDocument Class
Represents a PDF document and can be used to create a new PDF document from the scratch
Implements
Inherited Members
Namespace: Syncfusion.Pdf
Assembly: Syncfusion.Pdf.Base.dll
Syntax
public class PdfDocument : PdfDocumentBase, IDisposable
Remarks
To know more about refer this link .
Examples
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Creates a new page.
PdfPage page = document.Pages.Add();
//Create font with Bold font style.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold) ;
//Draw text in the new page.
page.Graphics.DrawString("Essential PDF", font, PdfBrushes.Black, new PointF(10, 10));
//Save the document.
document.Save("Document.pdf");
//Close the document.
document.Close(true);
' Create a new PDF document.
Dim document As PdfDocument = New PdfDocument()
'Creates a new page.
Dim page As PdfPage = document.Pages.Add()
'Create font with Bold font style.
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold)
'Draw text in the new page.
page.Graphics.DrawString("Essential PDF", font, PdfBrushes.Black, New PointF(10, 10))
'Save the document.
document.Save("Document.pdf");
'Close the document.
document.Close(True)
Constructors
PdfDocument()
Initializes a new instance of the PdfDocument class.
Declaration
public PdfDocument()
Remarks
To know more about refer this link .
Examples
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Creates a new page.
PdfPage page = document.Pages.Add();
//Create font with Bold font style.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold) ;
//Draw text in the new page.
page.Graphics.DrawString("Essential PDF", font, PdfBrushes.Black, new PointF(10, 10));
//Save the document.
document.Save("Document.pdf");
//Close the document.
document.Close(true);
' Create a new PDF document.
Dim document As PdfDocument = New PdfDocument()
'Creates a new page.
Dim page As PdfPage = document.Pages.Add()
'Create font with Bold font style.
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold)
'Draw text in the new page.
page.Graphics.DrawString("Essential PDF", font, PdfBrushes.Black, New PointF(10, 10))
'Save the document.
document.Save("Document.pdf")
'Close the document.
document.Close(True)
See Also
PdfDocument(PdfConformanceLevel)
Initializes a new instance of the PdfDocument class with the specified PdfConformanceLevel.
Declaration
public PdfDocument(PdfConformanceLevel conformance)
Parameters
Type | Name | Description |
---|---|---|
PdfConformanceLevel | conformance | The conformance level. |
Remarks
Not Supported under medium trust environment. This Constructor is not supported in WinRT, Windows Phone, Xamarin, Universal Windows Platform and Silverlight. To know more about document conformance refer this link .
Examples
//Create a new PDF document.
PdfDocument document = new PdfDocument(PdfConformanceLevel.Pdf_A1B);
//Create a new page
PdfPage page = document.Pages.Add();
//Create font and font style.
PdfFont font = new PdfTrueTypeFont(new Font(FontFamily.GenericMonospace, 12f, FontStyle.Bold));
//Draw text in the new page.
page.Graphics.DrawString("Essential PDF", font, PdfBrushes.Black, new PointF(10, 10));
//Saves the document
document.Save("Document.pdf");
Close the document
document.Close(true);
'Create a new PDF document.
Dim document As PdfDocument = New PdfDocument(PdfConformanceLevel.Pdf_A1B)
'Creates a new page
Dim page As PdfPage = document.Pages.Add()
'Create font and font style.
Dim font As PdfFont = New PdfTrueTypeFont(New Font(FontFamily.GenericMonospace, 12f, FontStyle.Bold))
'Draw text in the new page.
page.Graphics.DrawString("Essential PDF", font, PdfBrushes.Black, New PointF(10, 10))
'Saves the document
document.Save("Document.pdf")
'Close the document
document.Close(True)
See Also
Properties
Actions
Gets the actions to be performed when the document is opened/closed
Declaration
public PdfDocumentActions Actions { get; }
Property Value
Type | Description |
---|---|
PdfDocumentActions | The PdfDocumentActions specifying the document action. |
Examples
//Create a new PDF document
PdfDocument document = new PdfDocument();
//Create and add new launch Action to the document
PdfLaunchAction action = new PdfLaunchAction("input.txt", PdfFilePathType.Relative);
document.Actions.AfterOpen = action;
//Save the document
document.Save("Output.pdf");
//Close the document
document.Close(true);
'Create a new PDF document.
Dim document As PdfDocument = New PdfDocument()
'Create and add new launch Action to the document.
Dim action As PdfLaunchAction = New PdfLaunchAction("myAction.txt", PdfFilePathType.Relative)
document.Actions.AfterOpen = action
'Save the document
document.Save("Output.pdf")
'Close the document
document.Close(True)
See Also
Attachments
Gets the attachment collection of the document.
Declaration
public PdfAttachmentCollection Attachments { get; }
Property Value
Type | Description |
---|---|
PdfAttachmentCollection | The PdfAttachmentCollection object contains list of files which are attached in the PDF document. |
Remarks
To know more about refer this link .
Examples
//Create a new document.
PdfDocument doc = new PdfDocument();
// Creates a new page in document
PdfPage page = doc.Pages.Add();
// Adding an image as attachment
PdfAttachment attachment = new PdfAttachment("Logo.jpg");
attachment.Description = "Syncfusion Logo";
attachment.MimeType = "application/jpeg";
// Add the attachment in document
doc.Attachments.Add(attachment);
//Save the document
doc.Save("Attachment.pdf");
//Close the document
doc.Close(true);
' Create a new document.
Dim doc As PdfDocument = New PdfDocument()
' Creates a new page in document
Dim page As PdfPage = doc.Pages.Add()
' Adding an image as attachment
Dim attachment As PdfAttachment = New PdfAttachment("Logo.jpg")
attachment.Description = "Syncfusion Logo"
attachment.MimeType = "application/jpeg"
' Add the attachment in document
doc.Attachments.Add(attachment)
'Save the document
doc.Save("Attachment.pdf")
'Close the document
doc.Close(True)
See Also
AutoTag
Gets or sets a value indicates whether the document is auto-tagged or not.
Declaration
public bool AutoTag { get; set; }
Property Value
Type |
---|
System.Boolean |
Remarks
To know more about refer this link .
Examples
//Create a new document.
PdfDocument document = new PdfDocument();
//Set auto tag value.
document.AutoTag = true;
//Add new pdf page.
PdfPage page = document.Pages.Add();
//Initialize new instance of structure element with tag type heading.
PdfStructureElement header = new PdfStructureElement(PdfTagType.Heading);
//Initialize new instance of structure element with tag type HeadingLevel1.
PdfStructureElement header1 = new PdfStructureElement(PdfTagType.HeadingLevel1);
header1.Parent = header;
//Initialize new instance of structure element with tag type Paragraph.
PdfStructureElement structElement = new PdfStructureElement(PdfTagType.Paragraph);
structElement.Parent = header1;
//Create PDF text element.
PdfTextElement element = new PdfTextElement(@"Syncfusion Essential studio tool");
element.PdfTag = structElement;
//Set font for text element.
element.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12);
//Set brush for text element.
element.Brush = new PdfSolidBrush(new PdfColor(89, 89, 93));
//Draw text element into pdf page.
element.Draw(page, new RectangleF(0, 0, page.Graphics.ClientSize.Width / 2, 200));
//Save and close the document.
document.Save("Output.pdf");
document.Close(true);
'Create a new document.
Dim document As PdfDocument = New PdfDocument()
'Set auto tag value.
document.AutoTag = True
'Add new pdf page.
Dim page As PdfPage = document.Pages.Add()
'Initialize new instance of structure element with tag type heading.
Dim header As PdfStructureElement = New PdfStructureElement(PdfTagType.Heading)
'Initialize new instance of structure element with tag type HeadingLevel1.
Dim header1 As PdfStructureElement = New PdfStructureElement(PdfTagType.HeadingLevel1)
header1.Parent = header
'Initialize new instance of structure element with tag type Paragraph.
Dim structElement As PdfStructureElement = New PdfStructureElement(PdfTagType.Paragraph)
structElement.Parent = header1
'Create PDF text element.
Dim element As PdfTextElement = New PdfTextElement("Syncfusion Essential studio tool")
element.PdfTag = structElement
'Set font for text element.
element.Font = New PdfStandardFont(PdfFontFamily.TimesRoman, 12)
'Set brush for text element.
element.Brush = New PdfSolidBrush(New PdfColor(89, 89, 93))
'Draw text element into pdf page.
element.Draw(page, New RectangleF(0, 0, page.Graphics.ClientSize.Width / 2, 200))
'Save and Close the document.
document.Save("Output.pdf")
document.Close(True)
Bookmarks
Gets the bookmark collection of the document.
Declaration
public override PdfBookmarkBase Bookmarks { get; }
Property Value
Type | Description |
---|---|
PdfBookmarkBase | A PdfBookmarkBase specifies the document's bookmark. |
Overrides
Remarks
To know more about refer this link .
Examples
//Create a new document.
PdfDocument doc = new PdfDocument();
//Creates a new page
PdfPage page = doc.Pages.Add();
//Create outline
PdfBookmark bookMark = doc.Bookmarks.Add("InteractiveFeature");
bookMark.Color = Color.DarkBlue;
bookMark.TextStyle = PdfTextStyle.Bold;
bookMark.Title = "Interactive Feature";
bookMark.Destination = new PdfDestination(page);
//Save the document
doc.Save("Output.pdf");
//Close the document
doc.Close(true);
'Create a new document.
Dim doc As PdfDocument = New PdfDocument()
'Create a new page
Dim page As PdfPage = doc.Pages.Add()
'Create outline
Dim bookMark As PdfBookmark = document.Bookmarks.Add("InteractiveFeature")
bookMark.Color = Color.DarkBlue
bookMark.TextStyle = PdfTextStyle.Bold
bookMark.Title = "Interactive Feature"
bookMark.Destination = New PdfDestination(page)
doc.Save("Bookmarks.pdf")
//Close the document
doc.Close(True)
See Also
ColorSpace
Gets or sets the color space of the document. This property can be used to create PDF document in RGB, Grayscale or CMYK color spaces. By default the document uses RGB color space.
Declaration
public PdfColorSpace ColorSpace { get; set; }
Property Value
Type | Description |
---|---|
PdfColorSpace | The PdfColorSpace of the document. |
Remarks
This property has impact on the new created pages only.
Examples
//Create a new document.
PdfDocument doc = new PdfDocument();
//Creates a new page
PdfPage page = doc.Pages.Add();
// Set the document`s color spaces as GrayScale
doc.ColorSpace = PdfColorSpace.GrayScale;
doc.Save("ColorSpace.pdf");
//Close the document
doc.Close(true);
'Create a new document.
Dim doc As PdfDocument = New PdfDocument()
'Create a page
Dim page As PdfPage = doc.Pages.Add()
' Set the document`s color spaces as GrayScale
doc.ColorSpace = PdfColorSpace.GrayScale
doc.Save("ColorSpace.pdf")
'Close the document
doc.Close(True)
Conformance
Gets or sets the PDF conformance level. This property can be used to create PDF/A-1b or Pdf_X1A2001 compliant documents. By default is None.
Declaration
public PdfConformanceLevel Conformance { get; }
Property Value
Type | Description |
---|---|
PdfConformanceLevel | The PdfConformanceLevel ENUM. |
Remarks
Not Supported under Medium Trust environment. To know more about refer this link .
Examples
//Create a new PDF document.
PdfDocument document = new PdfDocument(PdfConformanceLevel.Pdf_A1B);
//Creates a new page and adds it as the last page of the document to the document.
PdfPage page = document.Pages.Add();
// Create a 'Times New Roman' font
Font font = new Font("Times New Roman", 10);
// Create font with bold font style.
PdfFont pdfFont = new PdfTrueTypeFont(font, false);
//Draw text in the new page.
page.Graphics.DrawString("Essential PDF", pdfFont, PdfBrushes.Black, new PointF(10, 10));
//Save document to disk.
document.Save("ConformanceLevel.pdf");
//Close the document
document.Close(true);
'Create a new PDF document.
Dim document As PdfDocument = New PdfDocument(PdfConformanceLevel.Pdf_A1B)
' Create a page to the document.
Dim page As PdfPage = document.Pages.Add()
' Create a 'Times New Roman' font
Dim font As Font = New Font("Times New Roman", 10)
' Create font with bold font style.
Dim pdfFont As PdfFont = New PdfTrueTypeFont(font, False)
'Draw text in the new page.
page.Graphics.DrawString("Essential PDF", pdfFont, PdfBrushes.Black, New PointF(10, 10))
'Save document to disk.
document.Save("ConformanceLevel.pdf")
'Close the document
document.Close(True)
EnableCache
Gets or sets the enable cache. This property is used to cache the fonts. Default value is True.
Declaration
public static bool EnableCache { get; set; }
Property Value
Type |
---|
System.Boolean |
Examples
//Create a new document.
PdfDocument doc = new PdfDocument();
//Diable the font cache
PdfDocument.EnableCache = false;
//Creates a new page and adds it as the last page of the document
PdfPage page = doc.Pages.Add();
//Create Pdf graphics for the page
PdfGraphics g = page.Graphics;
//Create a solid brush
PdfBrush brush = new PdfSolidBrush(Color.Black);
float fontSize = 20f;
//Set the font
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, fontSize);
//Draw the text
g.DrawString("Hello world!", font, brush, new PointF(20, 20));
doc.Save("Output.pdf");
//Close the document
doc.Close(true);
'Create a new document.
Dim doc As New PdfDocument()
'Diable the font cache
PdfDocument.EnableCache = False
'Creates a new page and adds it as the last page of the document
Dim page As PdfPage = doc.Pages.Add()
'Create Pdf graphics for the page
Dim g As PdfGraphics = page.Graphics
'Create a solid brush
Dim brush As PdfBrush = New PdfSolidBrush(Color.Black)
Dim fontSize As Single = 20.0F
'Set the font
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, fontSize)
'Draw the text
g.DrawString("Hello world!", font, brush, New PointF(20, 20))
doc.Save("Output.pdf")
'Close the document
doc.Close(True)
EnableThreadSafe
Gets or sets the enable thread safe. This property is used to manage the PDF cache objects in multithreading. Default value is false.
Declaration
public static bool EnableThreadSafe { get; set; }
Property Value
Type |
---|
System.Boolean |
Remarks
To know more about refer this link
Examples
//Set document range to be generated.
IEnumerable<int> works = Enumerable.Range(0, 100);
//Create PDF document in multi threading environment.
Parallel.ForEach(works, index => GeneratePDF(index));
//Method for create PDF document.
private static void GeneratePDF(int index)
{
//Enable the thread safe in PDF document.
PdfDocument.EnableThreadSafe = true;
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Create new instance for PDF standard font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
//Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));
string name = Guid.NewGuid().ToString();
//Save the document.
document.Save(name + ".pdf");
//Close the document.
document.Close(true);
}
' Set document range to be generated.
Dim works As IEnumerable(Of Integer) = Enumerable.Range(0, 100)
' Create PDF document in multi-threading environment.
Parallel.ForEach(works, Sub(index) GeneratePDF(index))
' Method for creating a PDF document.
Private Sub GeneratePDF(index As Integer)
' Enable the thread-safe mode in PDF document.
PdfDocument.EnableThreadSafe = True
' Create a new PDF document.
Dim document As New PdfDocument()
' Add a page to the document.
Dim page As PdfPage = document.Pages.Add()
' Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics
' Create a new instance for the PDF standard font.
Dim font As New PdfStandardFont(PdfFontFamily.Helvetica, 20)
' Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, New PointF(0, 0))
Dim name As String = Guid.NewGuid().ToString()
' Save the document.
document.Save(name + ".pdf")
' Close the document.
document.Close(True)
End Sub
EnableUniqueResourceNaming
Gets or sets boolean value which enables or disables unique resource naming, Default value is true.
Declaration
public static bool EnableUniqueResourceNaming { get; set; }
Property Value
Type |
---|
System.Boolean |
Remarks
By default this value will be true, if the uniform resource naming is needed then this value can be set false.
Examples
//Disables unique resource naming.
PdfDocument.EnableUniqueResourceNaming = false;
//Create a new PDF document.
PdfDocument doc = new PdfDocument();
//Add a page to the document.
PdfPage page = doc.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the colorspace for the document.
graphics.ColorSpace = PdfColorSpace.CMYK;
//Set the transparency of this graphics.
graphics.SetTransparency(0.5f);
//Create new instance for PDF font.
PdfFont font1 = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
//Draw the text.
graphics.DrawString("Hello World!!!", font1, PdfBrushes.Blue, new PointF(50, 50));
//Create new instance for PDF font.
PdfFont font2 = new PdfTrueTypeFont(new Font("Arial", 20), true);
//Draw the text.
graphics.DrawString("Hello World!!!", font2, PdfBrushes.Blue, new PointF(50, 100));
//Create new instance for PDF font.
PdfFont font3 = new PdfCjkStandardFont(PdfCjkFontFamily.HeiseiMinchoW3, 20);
//Draw the text.
graphics.DrawString("こんにちは世界", font3, PdfBrushes.Blue, new PointF(50, 150));
//Save and close the document.
doc.Save("Output.pdf");
doc.Close(true);
'Disables unique resource naming.
PdfDocument.EnableUniqueResourceNaming = False
'Create a new PDF document.
Dim doc As PdfDocument = New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = doc.Pages.Add()
'Create PDF graphics for the page.
Dim graphics As PdfGraphics = page.Graphics
'Set the colorspace for the document.
graphics.ColorSpace = PdfColorSpace.CMYK
'Set the transparency of this graphics
graphics.SetTransparency(0.5F)
'Create new instance for PDF font.
Dim font1 As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 20)
'Draw the text.
graphics.DrawString("Hello World!!!", font1, PdfBrushes.Blue, New PointF(50, 50))
'Create new instance for PDF font.
Dim font2 As PdfFont = New PdfTrueTypeFont(New Font("Arial", 20), True)
'Draw the text.
graphics.DrawString("Hello World!!!", font2, PdfBrushes.Blue, New PointF(50, 100))
'Create new instance for PDF font.
Dim font3 As PdfFont = New PdfCjkStandardFont(PdfCjkFontFamily.HeiseiMinchoW3, 20)
'Draw the text.
graphics.DrawString("こんにちは世界", font3, PdfBrushes.Blue, New PointF(50, 150))
'Save and close the document.
doc.Save("Output.pdf")
doc.Close(True)
Form
Gets the interactive form of the document.
Declaration
public PdfForm Form { get; }
Property Value
Type | Description |
---|---|
PdfForm | The PdfForm object contains the list of form elements of the document. |
Remarks
To know more about refer this link .
Examples
//Create a document.
PdfDocument document = new PdfDocument();
//Creates a new page.
PdfPage page = document.Pages.Add();
PdfFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 14);
//Create a text box.
PdfTextBoxField firstNameTextBox = new PdfTextBoxField(page, "firstNameTextBox");
firstNameTextBox.Bounds = new RectangleF(100, 20, 200, 20);
firstNameTextBox.Font = font;
//Add the textbox in form.
document.Form.Fields.Add(firstNameTextBox);
//Save the document.
document.Save("Form.pdf");
//Close the document.
document.Close(true);
'Create a document.
Dim document As PdfDocument = New PdfDocument()
'Create a page.
Dim page As PdfPage = document.Pages.Add()
'Create a text box.
Dim firstNameTextBox As PdfTextBoxField = New PdfTextBoxField(page, "firstNameTextBox")
firstNameTextBox.Bounds = New RectangleF(100, 20, 200, 20)
firstNameTextBox.Font = font
'Add the textbox in form.
document.Form.Fields.Add(firstNameTextBox)
document.Save("Form.pdf")
'Close the document.
document.Close(True)
NamedDestinationCollection
Gets the named destination collection of the document.
Declaration
public PdfNamedDestinationCollection NamedDestinationCollection { get; }
Property Value
Type | Description |
---|---|
PdfNamedDestinationCollection | A PdfNamedDestinationCollection specifies the document's named destinations. |
Remarks
To know more about refer this link .
Examples
//Create a new document.
PdfDocument doc = new PdfDocument();
//Creates a new page
PdfPage page = doc.Pages.Add();
//Create named destination
PdfNamedDestination namedDestination = new PdfNamedDestination("Interactive Feature");
namedDestination.Title = "Interactive Feature";
namedDestination.Destination = new PdfDestination(page);
doc.NamedDestinationCollection.Add(namedDestination);
//Save the document
doc.Save("NamedDestination.pdf");
//Close the document
doc.Close(true);
'Create a new document.
Dim doc As PdfDocument = New PdfDocument()
'Create a new page
Dim page As PdfPage = doc.Pages.Add()
'Create named destination
Dim namedDestination As PdfNamedDestination = New PdfNamedDestination("Interactive Feature")
namedDestination.Title = "Interactive Feature"
namedDestination.Destination = New PdfDestination(page)
doc.NamedDestinationCollection.Add(namedDestination)
doc.Save("NamedDestination.pdf")
//Close the document
doc.Close(True)
See Also
Pages
Gets the collection of pages in the document.
Declaration
public PdfDocumentPageCollection Pages { get; }
Property Value
Type | Description |
---|---|
PdfDocumentPageCollection | A PdfDocumentPageCollection object containing the list of document's pages. |
Examples
//Creates a new document.
PdfDocument doc = new PdfDocument();
//Creates a new page.
PdfPage page = doc.Pages.Add();
//Page collection.
PdfDocumentPageCollection pageCollection = doc.Pages;
PdfFont standardFont = new PdfStandardFont(PdfFontFamily.TimesRoman,10);
//Drawing string on first page.
pageCollection[0].Graphics.DrawString("FirstPage", standardFont, PdfBrushes.Black, new PointF(10, 10));
//Save the document.
doc.Save("Output.pdf");
//Close the document.
doc.Close(true);
'Creates a new document.
Dim doc As PdfDocument = New PdfDocument()
'Creates a new page.
Dim page As PdfPage = doc.Pages.Add()
'Page collection.
Dim pageCollection As PdfDocumentPageCollection = doc.Pages
Dim standardFont As PdfFont = New PdfStandardFont(PdfFontFamily.TimesRoman,10)
'Drawing string on the page.
pageCollection(0).Graphics.DrawString("FirstPage", standardFont, PdfBrushes.Black, New PointF(10, 10))
'Save the document.
doc.Save("Output.pdf")
'Close the document.
doc.Close(True)
PageSettings
Gets or sets the document's page setting.
Declaration
public PdfPageSettings PageSettings { get; set; }
Property Value
Type |
---|
PdfPageSettings |
Remarks
The changing of the settings doesn't take any effect on the already existing pages. To know more about refer this link .
Examples
//Create a new document class object.
PdfDocument doc = new PdfDocument();
//Creates a new page.
PdfPage page = doc.Pages.Add();
//Set page size.
doc.PageSettings.Size = PdfPageSize.A6;
//Set page orientation.
doc.PageSettings.Orientation = PdfPageOrientation.Landscape;
doc.Save("PageSettings.pdf");
//Close the document.
doc.Close(true);
'Create a new document class object.
Dim doc As PdfDocument = New PdfDocument()
'Create a page.
Dim page As PdfPage = doc.Pages.Add()
'Set page size.
doc.PageSettings.Size = PdfPageSize.A6
'Set page orientation.
doc.PageSettings.Orientation = PdfPageOrientation.Landscape
doc.Save("PageSettings.pdf")
'Close the document.
doc.Close(True)
Sections
Gets the collection of the sections in the document.
Declaration
public PdfSectionCollection Sections { get; }
Property Value
Type | Description |
---|---|
PdfSectionCollection | A PdfSectionCollection object containing the list of document's sections. |
Remarks
To know more about refer this link .
Examples
//Create a PDF document
PdfDocument doc = new PdfDocument();
// Create a new section
PdfSection mySection = doc.Sections.Add();
//Creates a new page
mySection.Pages.Add();
// Gets the section collection
PdfSectionCollection sectionCollection = doc.Sections;
// Gets the first page from first section
PdfPage page = sectionCollection[0].Pages[0];
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12);
// Draw the string in first page of the section
page.Graphics.DrawString("Hello World", font, PdfBrushes.Black, new Point(100, 100));
doc.Save("Sections.pdf");
//Close the document
doc.Close(true);
'Create a PDF document
Dim doc As PdfDocument = New PdfDocument()
' Create a new section
Dim mySection As PdfSection = doc.Sections.Add()
'Creates a new page
mySection.Pages.Add()
' Gets the section collection
Dim sectionCollection As PdfSectionCollection = doc.Sections
' Gets the first page from first section
Dim page As PdfPage = sectionCollection(0).Pages(0)
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 12)
' Draw the string in first page of the section
page.Graphics.DrawString("Hello World", font, PdfBrushes.Black, New Point(100, 100))
doc.Save("Sections.pdf")
'Close the document
doc.Close(True)
Template
Gets or sets a template to all pages in the document.
Declaration
public PdfDocumentTemplate Template { get; set; }
Property Value
Type | Description |
---|---|
PdfDocumentTemplate | The PdfDocumentTemplate specifying the default template for the document. |
Examples
//Create a PDF document.
PdfDocument doc = new PdfDocument();
//Creates a new page.
PdfPage page = doc.Pages.Add();
RectangleF rect = new RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height);
//Creates a new page.
PdfPageTemplateElement footer = new PdfPageTemplateElement(rect);
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 8);
PdfSolidBrush brush = new PdfSolidBrush(Color.Gray);
//Create page number field.
PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush);
//Create page count field.
PdfPageCountField count = new PdfPageCountField(font, brush);
PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count);
compositeField.Bounds = footer.Bounds;
compositeField.Draw(footer.Graphics, new PointF(40, footer.Height - 50));
//Add the footer template at the bottom.
doc.Template.Bottom = footer;
//Save the document.
doc.Save("Template.pdf");
//Close the document.
doc.Close(true);
'Create a PDF document.
Dim doc As PdfDocument = New PdfDocument()
'Create a page.
Dim page As PdfPage = doc.Pages.Add()
Dim rect As RectangleF = New RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height)
'Create a page template.
Dim footer As PdfPageTemplateElement = New PdfPageTemplateElement(rect)
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 8)
Dim brush As PdfSolidBrush = New PdfSolidBrush(Color.Gray)
'Create page number field.
Dim pageNumber As PdfPageNumberField = New PdfPageNumberField(font, brush)
'Create page count field.
Dim count As PdfPageCountField = New PdfPageCountField(font, brush)
Dim compositeField As PdfCompositeField = New PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count)
compositeField.Bounds = footer.Bounds
compositeField.Draw(footer.Graphics, New PointF(40, footer.Height - 50))
'Add the footer template at the bottom.
doc.Template.Bottom = footer
doc.Save("Template.pdf")
'Close the document.
doc.Close(True)
See Also
ZugferdConformanceLevel
Get or set the ZugferdConformanceLevel
Declaration
public ZugferdConformanceLevel ZugferdConformanceLevel { get; set; }
Property Value
Type |
---|
ZugferdConformanceLevel |
Remarks
It supports only in PdfConformanceLevel Pdf_A3B
Examples
//Creates a new PDF document.
PdfDocument doc = new PdfDocument(PdfConformanceLevel.Pdf_A3B);
//Add a page.
PdfPage page = doc.Pages.Add();
//Set the ZugferdProfile.
doc.ZugferdConformanceLevel = ZugferdConformanceLevel.Basic;
//Create the PdfTrueTypeFont
PdfFont font = new PdfTrueTypeFont(new Font("arial", 12f, FontStyle.Regular), true);
page.Graphics.DrawString("Hello World", font, PdfBrushes.Black, PointF.Empty);
//Creates an attachment
PdfAttachment attachment = new PdfAttachment("ZUGFeRD-invoice.xml");
//Set the file relationship
attachment.Relationship = PdfAttachmentRelationship.Alternative;
attachment.ModificationDate = DateTime.Now;
attachment.Description = "About Syncfusion";
attachment.MimeType = "application/xml";
//add attachment to the doucment
doc.Attachments.Add(attachment);
doc.Save("output.pdf");
'Creates a new PDF document.
Dim doc As New PdfDocument(PdfConformanceLevel.Pdf_A3B)
'Add a page.
Dim page As PdfPage = doc.Pages.Add()
'Set the ZugferdProfile.
doc.ZugferdConformanceLevel = ZugferdConformanceLevel.Basic
'Create the PdfTrueTypeFont
Dim font As PdfFont = New PdfTrueTypeFont(New Font("arial", 12F, FontStyle.Regular), True)
page.Graphics.DrawString("Hello World", font, PdfBrushes.Black, PointF.Empty)
'Creates an attachment
Dim attachment As New PdfAttachment("ZUGFeRD-invoice.xml")
'Set the file relationship
attachment.Relationship = PdfAttachmentRelationship.Alternative
attachment.ModificationDate = DateTime.Now
attachment.Description = "About Syncfusion"
attachment.MimeType = "application/xml"
'add attachment to the doucment
doc.Attachments.Add(attachment)
doc.Save("output.pdf")
ZugferdVersion
Get or set the ZUGFeRD version to the PDF
Declaration
public ZugferdVersion ZugferdVersion { get; set; }
Property Value
Type |
---|
ZugferdVersion |
Remarks
It supports only in PdfConformanceLevel Pdf_A3B
Examples
//Creates a new PDF document.
PdfDocument doc = new PdfDocument(PdfConformanceLevel.Pdf_A3B);
//Add a page.
PdfPage page = doc.Pages.Add();
//Set the ZugferdProfile.
doc.ZugferdVersion = ZugferdVersion.ZugferdVersion1_0;
//Set the ZugferdProfile.
doc.ZugferdConformanceLevel = ZugferdConformanceLevel.Basic;
//Create the PdfTrueTypeFont
PdfFont font = new PdfTrueTypeFont(new Font("arial", 12f, FontStyle.Regular), true);
page.Graphics.DrawString("Hello World", font, PdfBrushes.Black, PointF.Empty);
//Creates an attachment
PdfAttachment attachment = new PdfAttachment("ZUGFeRD-invoice.xml");
//Set the file relationship
attachment.Relationship = PdfAttachmentRelationship.Alternative;
attachment.ModificationDate = DateTime.Now;
attachment.Description = "About Syncfusion";
attachment.MimeType = "application/xml";
//add attachment to the doucment
doc.Attachments.Add(attachment);
doc.Save("output.pdf");
'Creates a new PDF document.
Dim doc As New PdfDocument(PdfConformanceLevel.Pdf_A3B)
'Add a page.
Dim page As PdfPage = doc.Pages.Add()
'Set the ZugferdProfile.
doc.ZugferdVersion = ZugferdVersion.ZugferdVersion1_0
'Set the ZugferdProfile.
doc.ZugferdConformanceLevel = ZugferdConformanceLevel.Basic
'Create the PdfTrueTypeFont
Dim font As PdfFont = New PdfTrueTypeFont(New Font("arial", 12F, FontStyle.Regular), True)
page.Graphics.DrawString("Hello World", font, PdfBrushes.Black, PointF.Empty)
'Creates an attachment
Dim attachment As New PdfAttachment("ZUGFeRD-invoice.xml")
'Set the file relationship
attachment.Relationship = PdfAttachmentRelationship.Alternative
attachment.ModificationDate = DateTime.Now
attachment.Description = "About Syncfusion"
attachment.MimeType = "application/xml"
'add attachment to the doucment
doc.Attachments.Add(attachment)
doc.Save("output.pdf")
Methods
ClearFontCache()
Releases all the font resources in multi-threading
Declaration
public static void ClearFontCache()
Clone()
Creates a new object that is a copy of the current instance.
Declaration
public object Clone()
Returns
Type | Description |
---|---|
System.Object | A new object that is a copy of this instance. |
Remarks
The resulting clone must be of the same type as or a compatible type to the original instance.
Examples
//Create a new document.
PdfDocument doc = new PdfDocument();
//Creates a new page and adds it as the last page of the document
PdfPage page = doc.Pages.Add();
//Create Pdf graphics for the page
PdfGraphics g = page.Graphics;
//Create a solid brush
PdfBrush brush = new PdfSolidBrush(Color.Black);
float fontSize = 20f;
//Set the font
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, fontSize);
//Draw the text
g.DrawString("Hello world!", font, brush,new PointF(20,20));
// Cloning the document
PdfDocument cloneDoc = doc.Clone() as PdfDocument;
cloneDoc.Save("Clone.pdf");
doc.Close(true);
'Create a new document.
Dim doc As PdfDocument = New PdfDocument()
'Create a page
Dim page As PdfPage = doc.Pages.Add()
'Create Pdf graphics for the page
Dim g As PdfGraphics = page.Graphics
'Create a solid brush
Dim brush As PdfBrush = New PdfSolidBrush(Color.Black)
Dim fontSize As Single = 20f
'Set the font
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, fontSize)
'Draw the text
g.DrawString("Hello world!", font, brush,New PointF(20,20))
' Cloning the document
Dim cloneDoc As PdfDocument = TryCast(doc.Clone(), PdfDocument)
cloneDoc.Save("Clone.pdf")
doc.Close(True)
Close(Boolean)
Close the document completely.
Declaration
public override void Close(bool completely)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | completely | if set to |
Overrides
Remarks
The document cannot be saved if Close method was invoked. To know more about refer this link .
Examples
//Create a new document.
PdfDocument doc = new PdfDocument();
//Creates a new page
PdfPage page = doc.Pages.Add();
//Create Pdf graphics for the page
PdfGraphics g = page.Graphics;
//Create a solid brush
PdfBrush brush = new PdfSolidBrush(Color.Black);
float fontSize = 20f;
//Set the font
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, fontSize);
//Draw the text
g.DrawString("Hello world!", font, brush,new PointF(20,20));
doc.Save("Sample.pdf");
// Closes the document.
doc.Close(true);
'Create a new document.
Dim doc As PdfDocument = New PdfDocument()
'Create a page
Dim page As PdfPage = doc.Pages.Add()
'Create Pdf graphics for the page
Dim g As PdfGraphics = page.Graphics
'Create a solid brush
Dim brush As PdfBrush = New PdfSolidBrush(Color.Black)
Dim fontSize As Single = 20f
'Set the font
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, fontSize)
'Draw the text
g.DrawString("Hello world!", font, brush,New PointF(20,20))
doc.Save("Sample.pdf")
' Closes the document.
doc.Close(True)
Dispose()
Releases all the resources used by document instances.
Declaration
public void Dispose()
Remarks
The document cannot be saved if Dispose method was invoked.
Examples
//Create a new document.
PdfDocument doc = new PdfDocument();
//Creates a new page
PdfPage page = doc.Pages.Add();
//Create Pdf graphics for the page
PdfGraphics g = page.Graphics;
//Loads an Image
Image img = Image.FromFile("input.png");
PdfImage pdfImg = new PdfBitmap(img);
//Draw the image
g.DrawImage(pdfImg,20, 20, 100,200);
//Save the document.
doc.Save("DisposeOnClose.pdf");
//Dispose the document
doc.Dispose();
'Create a new document.
Dim doc As PdfDocument = New PdfDocument()
'Create a page
Dim page As PdfPage = doc.Pages.Add()
'Create Pdf graphics for the page
Dim g As PdfGraphics = page.Graphics
'Loads an Image
Dim img As Image = Image.FromFile("Logo.png")
Dim pdfImg As PdfImage = New PdfBitmap(img)
'Draw the image
g.DrawImage(pdfImg,20, 20, 100,200)
'Save the document.
doc.Save("Dispose.pdf")
'Dispose the document
doc.Dispose()
Save(Stream)
Saves the document to the specified output stream.
Declaration
public override void Save(Stream stream)
Parameters
Type | Name | Description |
---|---|---|
System.IO.Stream | stream | The stream object where PDF document will be saved. |
Overrides
Examples
//Create a new document.
PdfDocument doc = new PdfDocument();
//Creates a new page and adds it as the last page of the document
PdfPage page = doc.Pages.Add();
//Create Pdf graphics for the page
PdfGraphics g = page.Graphics;
// Loads an Image
PdfImage pdfImg = new PdfBitmap(Image.FromFile("Logo.png"));
//Draw the image
g.DrawImage(pdfImg, 20, 20, 100, 200);
// Save the document as a stream
MemoryStream stream = new MemoryStream();
doc.Save(stream);
doc.Close(true);
'Create a new document.
Dim doc As PdfDocument = New PdfDocument()
'Create a page
Dim page As PdfPage = doc.Pages.Add()
'Create Pdf graphics for the page
Dim g As PdfGraphics = page.Graphics
' Loads an Image
Dim pdfImg As PdfImage = New PdfBitmap(Image.FromFile("Logo.png"))
'Draw the image
g.DrawImage(pdfImg, 20, 20, 100, 200)
' Save the document as a stream
Dim stream As MemoryStream = New MemoryStream()
doc.Save(stream)
'Close the document
doc.Close(True)