private async void GeneratePDF_Click(object sender, RoutedEventArgs e) { // Create a new document class object. PdfDocument doc = new PdfDocument(); PdfColor blackColor = new PdfColor(System.Drawing.Color.FromArgb(255, 0, 0, 0)); // Create few sections with one page in each. for (int i = 0; i < 4; ++i) { section = doc.Sections.Add(); //Create page label PdfPageLabel label = new PdfPageLabel(); label.Prefix = "Sec" + i + "-"; section.PageLabel = label; page = section.Pages.Add(); section.Pages[0].Graphics.SetTransparency(0.35f); section.PageSettings.Transition.PageDuration = 1; section.PageSettings.Transition.Duration = 1; section.PageSettings.Transition.Style = PdfTransitionStyle.Box; } //Set page size doc.PageSettings.Size = PdfPageSize.A6; //Set viewer prefernce. doc.ViewerPreferences.HideToolbar = true; doc.ViewerPreferences.PageMode = PdfPageMode.FullScreen; //Set page orientation doc.PageSettings.Orientation = PdfPageOrientation.Landscape; //Create a brush PdfSolidBrush brush = new PdfSolidBrush(blackColor); brush.Color = new PdfColor(System.Drawing.Color.FromArgb(255, 144, 238, 144)); //Create a Rectangle PdfRectangle rect = new PdfRectangle(0, 0, 1000f, 1000f); rect.Brush = brush; PdfPen pen = new PdfPen(blackColor); pen.Width = 6f; //Get the first page in first section page = doc.Sections[0].Pages[0]; //Draw the rectangle rect.Draw(page.Graphics); //Draw a line page.Graphics.DrawLine(pen, 0, 100, 300, 100); // Add margins. doc.PageSettings.SetMargins(0f); //Get the first page in second section page = doc.Sections[1].Pages[0]; doc.Sections[1].PageSettings.Rotate = PdfPageRotateAngle.RotateAngle90; rect.Draw(page.Graphics); page.Graphics.DrawLine(pen, 0, 100, 300, 100); // Change the angle f the section. This should rotate the previous page. doc.Sections[2].PageSettings.Rotate = PdfPageRotateAngle.RotateAngle180; page = doc.Sections[2].Pages[0]; rect.Draw(page.Graphics); page.Graphics.DrawLine(pen, 0, 100, 300, 100); section = doc.Sections[3]; section.PageSettings.Orientation = PdfPageOrientation.Portrait; page = section.Pages[0]; rect.Draw(page.Graphics); page.Graphics.DrawLine(pen, 0, 100, 300, 100); //Set the font PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16f); PdfSolidBrush fieldBrush = new PdfSolidBrush(blackColor); //Create page number field PdfPageNumberField pageNumber = new PdfPageNumberField(font, fieldBrush); //Create page count field PdfPageCountField count = new PdfPageCountField(font, fieldBrush); //Draw page template PdfPageTemplateElement templateElement = new PdfPageTemplateElement(400, 400); templateElement.Graphics.DrawString("Page :\tof", font, PdfBrushes.Black, new PointF(260, 200)); //Draw current page number pageNumber.Draw(templateElement.Graphics, new PointF(306, 200)); //Draw number of pages count.Draw(templateElement.Graphics, new PointF(345, 200)); doc.Template.Stamps.Add(templateElement); templateElement.Background = true; // Save and close the document. MemoryStream stream = new MemoryStream(); await doc.SaveAsync(stream); doc.Close(true); Save(stream, "PageSettings.pdf"); }
public ActionResult PageSettings(string InsideBrowser) { // Create a new document class object. PdfDocument doc = new PdfDocument(); PdfSection section; PdfPage pdfPage; // Create few sections with one page in each. for (int i = 0; i < 4; ++i) { section = doc.Sections.Add(); //Create page label PdfPageLabel label = new PdfPageLabel(); label.Prefix = "Sec" + i + "-"; section.PageLabel = label; pdfPage = section.Pages.Add(); section.Pages[0].Graphics.SetTransparency(0.35f); section.PageSettings.Transition.PageDuration = 1; section.PageSettings.Transition.Duration = 1; section.PageSettings.Transition.Style = PdfTransitionStyle.Box; } //Set page size doc.PageSettings.Size = PdfPageSize.A6; //Set viewer prefernce. doc.ViewerPreferences.HideToolbar = true; doc.ViewerPreferences.PageMode = PdfPageMode.FullScreen; //Set page orientation doc.PageSettings.Orientation = PdfPageOrientation.Landscape; //Create a brush PdfSolidBrush brush = new PdfSolidBrush(Color.Black); brush.Color = new PdfColor(Syncfusion.Drawing.Color.LightGreen); //Create a Rectangle PdfRectangle rect = new PdfRectangle(0, 0, 1000f, 1000f); rect.Brush = brush; PdfPen pen = new PdfPen(Syncfusion.Drawing.Color.Black); pen.Width = 6f; //Get the first page in first section pdfPage = doc.Sections[0].Pages[0]; //Draw the rectangle rect.Draw(pdfPage.Graphics); //Draw a line pdfPage.Graphics.DrawLine(pen, 0, 100, 300, 100); // Add margins. doc.PageSettings.SetMargins(0f); //Get the first page in second section pdfPage = doc.Sections[1].Pages[0]; doc.Sections[1].PageSettings.Rotate = PdfPageRotateAngle.RotateAngle90; rect.Draw(pdfPage.Graphics); pdfPage.Graphics.DrawLine(pen, 0, 100, 300, 100); // Change the angle f the section. This should rotate the previous page. doc.Sections[2].PageSettings.Rotate = PdfPageRotateAngle.RotateAngle180; pdfPage = doc.Sections[2].Pages[0]; rect.Draw(pdfPage.Graphics); pdfPage.Graphics.DrawLine(pen, 0, 100, 300, 100); section = doc.Sections[3]; section.PageSettings.Orientation = PdfPageOrientation.Portrait; pdfPage = section.Pages[0]; rect.Draw(pdfPage.Graphics); pdfPage.Graphics.DrawLine(pen, 0, 100, 300, 100); //Set the font PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16f); PdfSolidBrush fieldBrush = new PdfSolidBrush(Color.Black); //Create page number field PdfPageNumberField pageNumber = new PdfPageNumberField(font, fieldBrush); //Create page count field PdfPageCountField count = new PdfPageCountField(font, fieldBrush); //Draw page template PdfPageTemplateElement templateElement = new PdfPageTemplateElement(400, 400); templateElement.Graphics.DrawString("Page :\tof", font, PdfBrushes.Black, new PointF(260, 200)); //Draw current page number pageNumber.Draw(templateElement.Graphics, new PointF(306, 200)); //Draw number of pages count.Draw(templateElement.Graphics, new PointF(345, 200)); doc.Template.Stamps.Add(templateElement); templateElement.Background = true; //Save the PDF to the MemoryStream MemoryStream ms = new MemoryStream(); doc.Save(ms); //If the position is not set to '0' then the PDF will be empty. ms.Position = 0; //Close the PDF document. doc.Close(true); //Download the PDF document in the browser. FileStreamResult fileStreamResult = new FileStreamResult(ms, "application/pdf"); fileStreamResult.FileDownloadName = "PageSettings.pdf"; return(fileStreamResult); }
void DrawAutomaticField(String fieldName, PdfPageBase page, RectangleF bounds) { PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic)); PdfBrush brush = PdfBrushes.OrangeRed; PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); if ("DateTimeField" == fieldName) { PdfDateTimeField field = new PdfDateTimeField(); field.Font = font; field.Brush = brush; field.StringFormat = format; field.Bounds = bounds; field.DateFormatString = "yyyy-MM-dd HH:mm:ss"; field.Draw(page.Canvas); } if ("CreationDateField" == fieldName) { PdfCreationDateField field = new PdfCreationDateField(); field.Font = font; field.Brush = brush; field.StringFormat = format; field.Bounds = bounds; field.DateFormatString = "yyyy-MM-dd HH:mm:ss"; field.Draw(page.Canvas); } if ("DocumentAuthorField" == fieldName) { PdfDocumentAuthorField field = new PdfDocumentAuthorField(); field.Font = font; field.Brush = brush; field.StringFormat = format; field.Bounds = bounds; field.Draw(page.Canvas); } if ("SectionNumberField" == fieldName) { PdfSectionNumberField field = new PdfSectionNumberField(); field.Font = font; field.Brush = brush; field.StringFormat = format; field.Bounds = bounds; field.Draw(page.Canvas); } if ("SectionPageNumberField" == fieldName) { PdfSectionPageNumberField field = new PdfSectionPageNumberField(); field.Font = font; field.Brush = brush; field.StringFormat = format; field.Bounds = bounds; field.Draw(page.Canvas); } if ("SectionPageCountField" == fieldName) { PdfSectionPageCountField field = new PdfSectionPageCountField(); field.Font = font; field.Brush = brush; field.StringFormat = format; field.Bounds = bounds; field.Draw(page.Canvas); } if ("PageNumberField" == fieldName) { PdfPageNumberField field = new PdfPageNumberField(); field.Font = font; field.Brush = brush; field.StringFormat = format; field.Bounds = bounds; field.Draw(page.Canvas); } if ("PageCountField" == fieldName) { PdfPageCountField field = new PdfPageCountField(); field.Font = font; field.Brush = brush; field.StringFormat = format; field.Bounds = bounds; field.Draw(page.Canvas); } if ("DestinationPageNumberField" == fieldName) { PdfDestinationPageNumberField field = new PdfDestinationPageNumberField(); field.Font = font; field.Brush = brush; field.StringFormat = format; field.Bounds = bounds; field.Page = page as PdfNewPage; field.Draw(page.Canvas); } if ("CompositeField" == fieldName) { PdfSectionPageNumberField field1 = new PdfSectionPageNumberField(); field1.NumberStyle = PdfNumberStyle.LowerRoman; PdfSectionPageCountField field2 = new PdfSectionPageCountField(); PdfCompositeField fields = new PdfCompositeField(); fields.Font = font; fields.Brush = brush; fields.StringFormat = format; fields.Bounds = bounds; fields.AutomaticFields = new PdfAutomaticField[] { field1, field2 }; fields.Text = "section page {0} of {1}"; fields.Draw(page.Canvas); } }
private void button1_Click(object sender, System.EventArgs e) { // Create a new document class object. PdfDocument doc = new PdfDocument(); // Create few sections with one page in each. for (int i = 0; i < 4; ++i) { section = doc.Sections.Add(); //Create page label PdfPageLabel label = new PdfPageLabel(); label.Prefix = "Sec" + i + "-"; section.PageLabel = label; page = section.Pages.Add(); section.Pages[0].Graphics.SetTransparency(0.35f); section.PageSettings.Transition.PageDuration = 1; section.PageSettings.Transition.Duration = 1; section.PageSettings.Transition.Style = PdfTransitionStyle.Box; } //Set page size doc.PageSettings.Size = PdfPageSize.A6; //Set viewer prefernce. doc.ViewerPreferences.HideToolbar = true; doc.ViewerPreferences.PageMode = PdfPageMode.FullScreen; //Set page orientation doc.PageSettings.Orientation = PdfPageOrientation.Landscape; //Create a brush PdfSolidBrush brush = new PdfSolidBrush(Color.Black); brush.Color = new PdfColor(System.Drawing.Color.LightGreen); //Create a Rectangle PdfRectangle rect = new PdfRectangle(0, 0, 1000f, 1000f); rect.Brush = brush; PdfPen pen = new PdfPen(System.Drawing.Color.Black); pen.Width = 6f; //Get the first page in first section page = doc.Sections[0].Pages[0]; //Draw the rectangle rect.Draw(page.Graphics); //Draw a line page.Graphics.DrawLine(pen, 0, 100, 300, 100); // Add margins. doc.PageSettings.SetMargins(0f); //Get the first page in second section page = doc.Sections[1].Pages[0]; doc.Sections[1].PageSettings.Rotate = PdfPageRotateAngle.RotateAngle90; rect.Draw(page.Graphics); page.Graphics.DrawLine(pen, 0, 100, 300, 100); // Change the angle f the section. This should rotate the previous page. doc.Sections[2].PageSettings.Rotate = PdfPageRotateAngle.RotateAngle180; page = doc.Sections[2].Pages[0]; rect.Draw(page.Graphics); page.Graphics.DrawLine(pen, 0, 100, 300, 100); section = doc.Sections[3]; section.PageSettings.Orientation = PdfPageOrientation.Portrait; page = section.Pages[0]; rect.Draw(page.Graphics); page.Graphics.DrawLine(pen, 0, 100, 300, 100); //Set the font PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16f); PdfSolidBrush fieldBrush = new PdfSolidBrush(Color.Black); //Create page number field PdfPageNumberField pageNumber = new PdfPageNumberField(font, fieldBrush); //Create page count field PdfPageCountField count = new PdfPageCountField(font, fieldBrush); //Draw page template PdfPageTemplateElement templateElement = new PdfPageTemplateElement(400, 400); templateElement.Graphics.DrawString("Page :\tof", font, PdfBrushes.Black, new PointF(260, 200)); //Draw current page number pageNumber.Draw(templateElement.Graphics, new PointF(306, 200)); //Draw number of pages count.Draw(templateElement.Graphics, new PointF(345, 200)); doc.Template.Stamps.Add(templateElement); templateElement.Background = true; //Save the PDF doc.Save("Sample.pdf"); //Message box confirmation to view the created PDF document. if (MessageBox.Show("Do you want to view the PDF file?", "PDF File Created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { //Launching the PDF file using the default Application.[Acrobat Reader] #if NETCORE System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo = new System.Diagnostics.ProcessStartInfo("Sample.pdf") { UseShellExecute = true }; process.Start(); #else System.Diagnostics.Process.Start("Sample.pdf"); #endif this.Close(); } else { // Exit this.Close(); } }