In This Topic
Programming / PDF / Creating a PDF document from scratch

Creating a PDF document from scratch

In This Topic

The goal of this piece of code is to create a PDF document from scratch. It shows you how to add text, rectangles and form fields to the document and how to save the result.

Copy Code
'We assume that GdPicture has been correctly installed and unlocked.
'Firstly, we create a new PDF document.
Dim oGdPicturePDF As New GdPicturePDF()
Dim status As GdPictureStatus = oGdPicturePDF.NewPDF()
If status <> GdPictureStatus.OK Then
    MessageBox.Show("Error occurred whe creating a new PDF document. Error: " + status.ToString(), "Creating PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
    GoTo [end]
End If
'For convenience reasons, set the measurement units and the origin.
oGdPicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitMillimeter)
oGdPicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft)
'Creating and inserting a new A4 sized page to the document.
status = oGdPicturePDF.NewPage(210, 297)
If status <> GdPictureStatus.OK Then
    MessageBox.Show("Error occurred when creating a new page. Error: " + status.ToString(), "Creating PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
    GoTo [end]
End If
'Adding fonts that will be used in this PDF document.
Dim fontHelv As String = oGdPicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica)
If oGdPicturePDF.GetStat() <> GdPictureStatus.OK Then
    MessageBox.Show("Error occurred when adding fonts. Error: " + oGdPicturePDF.GetStat().ToString(), "Creating PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
    GoTo [end]
End If
Dim fontHelvBold As String = oGdPicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelveticaBold)
If oGdPicturePDF.GetStat() <> GdPictureStatus.OK Then
    MessageBox.Show("Error occurred when adding fonts. Error: " + oGdPicturePDF.GetStat().ToString(), "Creating PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
    GoTo [end]
End If
'Drawing the title in a blue rectangle and then drawing the page body.
If (oGdPicturePDF.SetLineColor(Color.Blue) = GdPictureStatus.OK) AndAlso
   (oGdPicturePDF.SetLineWidth(2) = GdPictureStatus.OK) AndAlso
   (oGdPicturePDF.DrawRoundedRectangle(10, 5, 190, 20, 3, False, True) = GdPictureStatus.OK) AndAlso
   (oGdPicturePDF.SetTextSize(30) = GdPictureStatus.OK) AndAlso
   (oGdPicturePDF.DrawTextBox(fontHelvBold, 10, 5, 200, 25, TextAlignment.TextAlignmentCenter, TextAlignment.TextAlignmentCenter, "Form Filling") = GdPictureStatus.OK) AndAlso
   (oGdPicturePDF.SetLineColor(Color.Black) = GdPictureStatus.OK) AndAlso
   (oGdPicturePDF.SetLineWidth(0.1F) = GdPictureStatus.OK) AndAlso
   (oGdPicturePDF.SetTextSize(10) = GdPictureStatus.OK) AndAlso
   (oGdPicturePDF.DrawText(fontHelv, 10, 40, "Name") = GdPictureStatus.OK) AndAlso
   (oGdPicturePDF.DrawText(fontHelv, 10, 50, "Nick Name") = GdPictureStatus.OK) AndAlso
   (oGdPicturePDF.DrawText(fontHelv, 10, 70, "Centre of interest") = GdPictureStatus.OK) AndAlso
   (oGdPicturePDF.DrawRectangle(5, 30, 200, 80, False, True) = GdPictureStatus.OK) Then
    'Adding some form fields.
    Dim FieldNameId As Integer = oGdPicturePDF.AddTextFormField(40, 35, 90, 8, "Name", "Enter your name", False, fontHelv, 8, Color.Black)
    If oGdPicturePDF.GetStat() <> GdPictureStatus.OK Then
        oGdPicturePDF.SetFormFieldMaxLen(FieldNameId, 50)
    Else
        MessageBox.Show("Error occurred when adding form Name. Error: " + oGdPicturePDF.GetStat().ToString(), "Creating PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
    Dim FieldNickNameId As Integer = oGdPicturePDF.AddTextFormField(40, 45, 90, 8, "Nickname", "Enter your nickname", False, fontHelv, 8, Color.Black)
    If oGdPicturePDF.GetStat() <> GdPictureStatus.OK Then
        oGdPicturePDF.SetFormFieldMaxLen(FieldNickNameId, 50)
    Else
        MessageBox.Show("Error occurred when adding form Nickname. Error: " + oGdPicturePDF.GetStat().ToString(), "Creating PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
    Dim FieldCOIId As Integer = oGdPicturePDF.AddTextFormField(40, 55, 90, 30, "COI", "Enter your centre of interest", True, fontHelv, 8, Color.Black)
    If oGdPicturePDF.GetStat() <> GdPictureStatus.OK Then
        oGdPicturePDF.SetFormFieldMaxLen(FieldCOIId, 500)
    Else
        MessageBox.Show("Error occurred when adding form COI. Error: " + oGdPicturePDF.GetStat().ToString(), "Creating PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
    'And finally, saving the PDF document.
    status = oGdPicturePDF.SaveToFile("form.pdf")
    MessageBox.Show("Finished! Status: " + status.ToString(), "Creating PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
    MessageBox.Show("Error occurred when drawing. Error: " + oGdPicturePDF.GetStat().ToString(), "Creating PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
[end]:
oGdPicturePDF.Dispose()
Copy Code
//We assume that GdPicture has been correctly installed and unlocked.
//Firstly, we create a new PDF document.
GdPicturePDF oGdPicturePDF = new GdPicturePDF();
GdPictureStatus status = oGdPicturePDF.NewPDF();
if (status != GdPictureStatus.OK)
{
    MessageBox.Show("Error occurred whe creating a new PDF document. Error: " + status.ToString(), "Creating PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
    goto end;
}
//For convenience reasons, set the measurement units and the origin.
oGdPicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitMillimeter);
oGdPicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft);
//Creating and inserting a new A4 sized page to the document.
status = oGdPicturePDF.NewPage(210, 297);
if (status != GdPictureStatus.OK)
{
    MessageBox.Show("Error occurred when creating a new page. Error: " + status.ToString(), "Creating PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
    goto end;
}
//Adding fonts that will be used in this PDF document.
string fontHelv = oGdPicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica);
if (oGdPicturePDF.GetStat() != GdPictureStatus.OK)
{
    MessageBox.Show("Error occurred when adding fonts. Error: " + oGdPicturePDF.GetStat().ToString(), "Creating PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
    goto end;
}
string fontHelvBold = oGdPicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelveticaBold);
if (oGdPicturePDF.GetStat() != GdPictureStatus.OK)
{
    MessageBox.Show("Error occurred when adding fonts. Error: " + oGdPicturePDF.GetStat().ToString(), "Creating PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
    goto end;
}
//Drawing the title in a blue rectangle.
if ((oGdPicturePDF.SetLineColor(Color.Blue) == GdPictureStatus.OK) &&
    (oGdPicturePDF.SetLineWidth(2) == GdPictureStatus.OK) &&
    (oGdPicturePDF.DrawRoundedRectangle(10, 5, 190, 20, 3, false, true) == GdPictureStatus.OK) &&
    (oGdPicturePDF.SetTextSize(30) == GdPictureStatus.OK) &&
    (oGdPicturePDF.DrawTextBox(fontHelvBold, 10, 5, 200, 25, TextAlignment.TextAlignmentCenter, TextAlignment.TextAlignmentCenter, "Form Filling") == GdPictureStatus.OK) &&
//Now the page body.
    (oGdPicturePDF.SetLineColor(Color.Black) == GdPictureStatus.OK) &&
    (oGdPicturePDF.SetLineWidth(0.1F) == GdPictureStatus.OK) &&
    (oGdPicturePDF.SetTextSize(10) == GdPictureStatus.OK) &&
    (oGdPicturePDF.DrawText(fontHelv, 10, 40, "Name") == GdPictureStatus.OK) &&
    (oGdPicturePDF.DrawText(fontHelv, 10, 50, "Nick Name") == GdPictureStatus.OK) &&
    (oGdPicturePDF.DrawText(fontHelv, 10, 70, "Centre of interest") == GdPictureStatus.OK) &&
    (oGdPicturePDF.DrawRectangle(5, 30, 200, 80, false, true) == GdPictureStatus.OK))
{
    //Adding some form fields.
    int FieldNameId = oGdPicturePDF.AddTextFormField(40, 35, 90, 8, "Name", "Enter your name", false, fontHelv, 8, Color.Black);
    if (oGdPicturePDF.GetStat() != GdPictureStatus.OK)
        oGdPicturePDF.SetFormFieldMaxLen(FieldNameId, 50);
    else
        MessageBox.Show("Error occurred when adding form Name. Error: " + oGdPicturePDF.GetStat().ToString(), "Creating PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
    int FieldNickNameId = oGdPicturePDF.AddTextFormField(40, 45, 90, 8, "Nickname", "Enter your nickname", false, fontHelv, 8, Color.Black);
    if (oGdPicturePDF.GetStat() != GdPictureStatus.OK)
        oGdPicturePDF.SetFormFieldMaxLen(FieldNickNameId, 50);
    else
        MessageBox.Show("Error occurred when adding form Nickname. Error: " + oGdPicturePDF.GetStat().ToString(), "Creating PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
    int FieldCOIId = oGdPicturePDF.AddTextFormField(40, 55, 90, 30, "COI", "Enter your centre of interest", true, fontHelv, 8, Color.Black);
    if (oGdPicturePDF.GetStat() != GdPictureStatus.OK)
        oGdPicturePDF.SetFormFieldMaxLen(FieldCOIId, 500);
    else
        MessageBox.Show("Error occurred when adding form COI. Error: " + oGdPicturePDF.GetStat().ToString(), "Creating PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
    //And finally, saving the PDF document.
    status = oGdPicturePDF.SaveToFile(@"form.pdf");
    MessageBox.Show("Finished! Status: " + status.ToString(), "Creating PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
    MessageBox.Show("Error occurred when drawing. Error: " + oGdPicturePDF.GetStat().ToString(), "Creating PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
end:
oGdPicturePDF.Dispose();