// Main sample entry: public int CreatePDF(Stream stream) { // Set up a font collection with the fonts we need: _fc.RegisterDirectory(Path.Combine("Resources", "Fonts")); // Get the PDF to add index to: string tfile = Path.Combine("Resources", "PDFs", "CompleteJavaScriptBook.pdf"); // The list of words on which we will build the index: var words = _keywords.Distinct(StringComparer.InvariantCultureIgnoreCase).Where(w_ => !string.IsNullOrEmpty(w_)); // Load the PDF and add the index: using (var fs = new FileStream(tfile, FileMode.Open, FileAccess.Read)) { var doc = new GcPdfDocument(); doc.Load(fs); // int origPageCount = doc.Pages.Count; // Build and add the index: AddWordIndex(doc, words); // Open document on the first index page by default // (may not work in browser viewers, but works in Acrobat): doc.OpenAction = new DestinationFit(origPageCount); // Done: doc.Save(stream); return(doc.Pages.Count); } }
// Main entry point of this sample: public int CreatePDF(Stream stream) { // Set up a font collection with the fonts we need: _fc.RegisterDirectory(Path.Combine("Resources", "Fonts")); // Set that font collection on input fields' text layout // (we will also set it on all text layouts that we'll use): _inputTl.FontCollection = _fc; // Set up layout and formatting for input fields: _inputTl.ParagraphAlignment = ParagraphAlignment.Center; _inputTf.FontName = "Segoe UI"; _inputTf.FontSize = 12; _inputTf.FontBold = true; // Create the time sheet input form // (in a real-life scenario, we probably would only create it once, // and then re-use the form PDF): var doc = MakeTimeSheetForm(); // Save the PDF: doc.Save(stream); // Images used in a document can be disposed only after saving the PDF: _logo.Dispose(); // Done: return(doc.Pages.Count); }
// Main entry point of this sample: public int CreatePDF(Stream stream) { var doc = new GcPdfDocument(); _fc.RegisterDirectory(Path.Combine("Resources", "Fonts")); // First page: Page1(doc); // Second page: Page2(doc); // Save the PDF: doc.Save(stream); // Dispose images after the document has been saved: _disposables.ForEach(d_ => d_.Dispose()); // Done: return(doc.Pages.Count); }
public void CreatePDF(Stream stream) { var fc = new FontCollection(); fc.RegisterDirectory(Path.Combine("Resources", "Fonts")); var doc = new GcPdfDocument(); var g = doc.NewPage().Graphics; var rc = Common.Util.AddNote( "TextFormat.FontStyle allows to turn on Bold and/or Italic emulation\n" + "on a regular (not a bold/italic) font.", doc.Pages.Last); // Text insertion point: PointF ip = new PointF(rc.Left, rc.Bottom + 36); TextFormat tf = new TextFormat(); // We specifically get a non-bold/non-italic version of the font: tf.Font = fc.FindFamilyName("Times New Roman", false, false); tf.FontSize = 16; g.DrawString($"Regular Times font: {tf.Font.FullFontName}", tf, ip); ip.Y += 36; // Draw some strings using the same (regular) font but emulating bold and italic: tf.FontStyle = FontStyle.Bold; g.DrawString($"Bold emulation using font: {tf.Font.FullFontName}", tf, ip); ip.Y += 36; tf.FontStyle = FontStyle.Italic; g.DrawString($"Italic emulation using font: {tf.Font.FullFontName}", tf, ip); ip.Y += 36; tf.FontStyle = FontStyle.BoldItalic; g.DrawString($"Bold+Italic emulation using font: {tf.Font.FullFontName}", tf, ip); ip.Y += 36; // // Now we render some strings using the "real" bold/italic variants of the font: tf.FontStyle = FontStyle.Regular; tf.Font = fc.FindFamilyName("Times New Roman", true, false); g.DrawString($"Using real bold font: {tf.Font.FullFontName}", tf, ip); ip.Y += 36; tf.Font = fc.FindFamilyName("Times New Roman", false, true); g.DrawString($"Using real italic font: {tf.Font.FullFontName}", tf, ip); ip.Y += 36; tf.Font = fc.FindFamilyName("Times New Roman", true, true); g.DrawString($"Using real bold italic font: {tf.Font.FullFontName}", tf, ip); ip.Y += 36; // Done: doc.Save(stream); }
public void CreatePDF(Stream stream) { var doc = new GcPdfDocument(); var page = doc.NewPage(); // Use Landscape orientation: page.Landscape = true; var g = page.Graphics; // Some sample texts in Japanese, English and Arabic: string text1 = "学校教育の「国語」で教えられる。"; string text2 = " flow direction. "; string text3 = "النص العربي 12 + 34 = 46 مع الأرقام "; // Init font cache and get the required fonts: var fc = new FontCollection(); fc.RegisterDirectory(Path.Combine("Resources", "Fonts")); var fYuMin = fc.FindFamilyName("Yu Mincho"); var fTimes = fc.FindFamilyName("Times New Roman"); var fArial = fc.FindFamilyName("Arial"); // Create text formats: var tf1 = new TextFormat() { Font = fYuMin }; var tf2 = new TextFormat() { Font = fTimes }; var tf3 = new TextFormat() { Font = fArial }; // Create TextLayout and set some options on it: var tl = g.CreateTextLayout(); tl.FirstLineIndent = 36; tl.TextAlignment = TextAlignment.Justified; // This setting justifies the last line too: tl.LastLineIsEndOfParagraph = false; // Set all margins to 1": tl.MarginAll = tl.Resolution; tl.MaxWidth = page.Size.Width; tl.MaxHeight = page.Size.Height; // RTL layout: tl.RightToLeft = false; // Build a list of objects for the text to flow around: tl.ObjectRects = new List <ObjectRect>() { new ObjectRect(540, 100, 120, 160), new ObjectRect(100, 290, 170, 100), new ObjectRect(500, 350, 170, 100) }; // Fill corresponding rectangels on page so that we can see them: foreach (var or in tl.ObjectRects) { g.FillRectangle(or.ToRectangleF(), Color.PaleVioletRed); } // Add text to layout: for (int i = 0; i < 3; i++) { tl.Append(text1, tf1); tl.Append("Horizontal Top To Bottom" + text2, tf2); tl.AppendLine(text3, tf3); } // Perform and draw first layout: tl.PerformLayout(true); g.DrawTextLayout(tl, PointF.Empty); g.FillRectangle(tl.ContentRectangle, Color.FromArgb(20, Color.Red)); // Create 2nd layout - vertical rotated counter-clockwise: var t = tl.ContentHeight; tl.Clear(); tl.RotateSidewaysCounterclockwise = true; tl.FlowDirection = FlowDirection.VerticalLeftToRight; tl.MarginTop += t; // Add text to layout: for (int i = 0; i < 3; i++) { tl.Append(text1, tf1); tl.Append("Vertical Left To Right" + text2, tf2); tl.AppendLine(text3, tf3); } // Perform and draw second layout: tl.PerformLayout(true); g.DrawTextLayout(tl, PointF.Empty); g.FillRectangle(tl.ContentRectangle, Color.FromArgb(20, Color.Green)); // Create 3rd layout - vertical: tl.Clear(); tl.FlowDirection = FlowDirection.VerticalRightToLeft; tl.RotateSidewaysCounterclockwise = false; // Add text to layout: for (int i = 0; i < 3; i++) { tl.Append(text1, tf1); tl.Append("Vertical Right To Left" + text2, tf2); tl.AppendLine(text3, tf3); } // Perform and draw third layout: tl.PerformLayout(true); g.DrawTextLayout(tl, PointF.Empty); g.FillRectangle(tl.ContentRectangle, Color.FromArgb(20, Color.Blue)); // Done: doc.Save(stream); }
// Main entry point of this sample: public int CreatePDF(Stream stream) { // Set up a font collection with the fonts we need: _fc.RegisterDirectory(Path.Combine("Resources", "Fonts")); // Set that font collection on input fields' text layout // (we will also set it on all text layouts that we'll use): _inputTl.FontCollection = _fc; // Set up layout and formatting for input fields: _inputTl.ParagraphAlignment = ParagraphAlignment.Center; _inputTf.FontName = "Segoe UI"; _inputTf.FontSize = 12; _inputTf.FontBold = true; // Create the time sheet input form // (in a real-life scenario, we probably would only create it once, // and then re-use the form PDF): var doc = MakeTimeSheetForm(); // At this point, 'doc' is an empty AcroForm. // In a real-life app it would be distributed to employees // for them to fill and send back. FillEmployeeData(doc); // // At this point the form is filled with employee's data. // // Supervisor data (in a real app, these would probably be fetched from a db): var supName = "Jane Donahue"; var supSignDate = DateTime.Now.ToShortDateString(); SetFieldValue(doc, _Names.EmpSuper, supName); SetFieldValue(doc, _Names.SupSignDate, supSignDate); // The next step is to 'flatten' the form: we loop over document AcroForm's fields, // drawing their current values in place, and then remove the fields. // This produces a PDF with text fields' values as part of the regular (non-editable) content: FlattenDoc(doc); // Now we digitally sign the flattened document on behalf of the 'manager': var pfxPath = Path.Combine("Resources", "Misc", "GcPdfTest.pfx"); X509Certificate2 cert = new X509Certificate2(File.ReadAllBytes(pfxPath), "qq", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable); SignatureProperties sp = new SignatureProperties(); sp.Certificate = cert; sp.Location = "GcPdfWeb - TimeSheet sample"; sp.SignerName = supName; // Connect the signature field and signature props: SignatureField supSign = doc.AcroForm.Fields.First(f_ => f_.Name == _Names.SupSign) as SignatureField; sp.SignatureField = supSign; supSign.Widget.ButtonAppearance.Caption = supName; // Some browser PDF viewers do not show form fields, so we render a placeholder: supSign.Widget.Page.Graphics.DrawString("digitally signed", new TextFormat() { FontName = "Segoe UI", FontSize = 9 }, supSign.Widget.Rect); // Done, now save the document with supervisor signature: doc.Sign(sp, stream); // Dispose images only after the document is saved: _disposables.ForEach(d_ => d_.Dispose()); return(doc.Pages.Count); }
// Main entry point of this sample: public int CreatePDF(Stream stream) { // Set up a font collection with the fonts we need: _fc.RegisterDirectory(Path.Combine("Resources", "Fonts")); // Set that font collection on input fields' text layout // (we will also set it on all text layouts that we'll use): _inputTl.FontCollection = _fc; // Set up layout and formatting for input fields: _inputTl.ParagraphAlignment = ParagraphAlignment.Center; _inputTf.FontName = "Segoe UI"; _inputTf.FontSize = 12; _inputTf.FontBold = true; // Create the time sheet input form // (in a real-life scenario, we probably would only create it once, // and then re-use the form PDF): var doc = MakeTimeSheetForm(); // At this point, 'doc' is an empty AcroForm. // In a real-life app it would be distributed to employees // for them to fill and send back. using (var empSignedStream = FillEmployeeData(doc)) { // // At this point 'empSignedStream' contains the form filled with employee's data and signed by them. // // Load the employee-signed document: doc.Load(empSignedStream); // Fill in supervisor data: var supName = "Jane Donahue"; var supSignDate = DateTime.Now.ToShortDateString(); SetFieldValue(doc, _Names.EmpSuper, supName); SetFieldValue(doc, _Names.SupSignDate, supSignDate); // Digitally sign the document on behalf of the supervisor: var pfxPath = Path.Combine("Resources", "Misc", "GcPdfTest.pfx"); X509Certificate2 cert = new X509Certificate2(File.ReadAllBytes(pfxPath), "qq", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable); SignatureProperties sp = new SignatureProperties() { Certificate = cert, Location = "GcPdfWeb - TimeSheet Incremental", SignerName = supName, SignatureDigestAlgorithm = SignatureDigestAlgorithm.SHA512, // Connect the signature field and signature props: SignatureField = doc.AcroForm.Fields.First(f_ => f_.Name == _Names.SupSign) as SignatureField, }; // Any changes to the document would invalidate the employee's signature, so we cannot do this: // supSign.Widget.ButtonAppearance.Caption = supName; // // Done, now save the document with supervisor signature: // NOTE: in order to not invalidate the employee's signature, // we MUST use incremental update here (which is true by default in Sign() method): doc.Sign(sp, stream); _logo.Dispose(); return(doc.Pages.Count); } }
public void CreatePDF(Stream stream) { // Function to generate sample text quoting its formatting options: Func <TextFormat, string> makeSampleText = (tf_) => { string boldItalic = string.Empty; if (tf_.Font.FontBold) { boldItalic = "bold "; } if (tf_.Font.FontItalic) { boldItalic += "italic "; } if (boldItalic == string.Empty) { boldItalic = "normal "; } return($"This is {boldItalic}text drawn using font '{tf_.Font.FullFontName}', font size {tf_.FontSize} points, " + $"text color {tf_.ForeColor}, background color {tf_.BackColor}. "); }; // Font names: const string times = "times new roman"; const string arial = "arial"; // Create document and text layout: var doc = new GcPdfDocument(); var page = doc.NewPage(); var g = page.Graphics; var tl = g.CreateTextLayout(); // Use TextLayout to layout the whole page and maintain margins: tl.MaxHeight = page.Size.Height; tl.MaxWidth = page.Size.Width; tl.MarginAll = 72; // Get some fonts: var fc = new FontCollection(); fc.RegisterDirectory(Path.Combine("Resources", "Fonts")); var fTimes = fc.FindFamilyName(times, false, false); var fTimesBold = fc.FindFamilyName(times, true, false); var fTimesItalic = fc.FindFamilyName(times, false, true); var fTimesBoldItalic = fc.FindFamilyName(times, true, true); var fArial = fc.FindFamilyName(arial, false, false); // Add text to TextLayout using different fonts and font sizes: TextFormat tf = new TextFormat() { Font = fTimes, FontSize = 12, }; tl.Append(makeSampleText(tf), tf); tf.Font = fTimesBold; tf.FontSize += 2; tl.Append(makeSampleText(tf), tf); tf.Font = fTimesItalic; tf.FontSize += 2; tl.Append(makeSampleText(tf), tf); tf.Font = fTimesBoldItalic; tf.FontSize += 2; tl.Append(makeSampleText(tf), tf); tf.Font = fArial; tf.FontSize += 2; tl.Append(makeSampleText(tf), tf); // Add text with different foreground and background colors: tf.Font = fTimesBold; tf.ForeColor = Color.Tomato; tl.Append(makeSampleText(tf), tf); tf.Font = fTimesBoldItalic; tf.FontSize = 16; tf.ForeColor = Color.SlateBlue; tf.BackColor = Color.Orange; tl.Append(makeSampleText(tf), tf); // Finish with plain black on transparent again: tl.Append("The end.", new TextFormat() { Font = fTimes, FontSize = 14, }); // Layout and draw text: tl.PerformLayout(true); g.DrawTextLayout(tl, PointF.Empty); // Done: doc.Save(stream); }
public void CreatePDF(Stream stream) { // Create a FontCollection instance: FontCollection fc = new FontCollection(); // Populate it with fonts from the specified directory: fc.RegisterDirectory(Path.Combine("Resources", "Fonts")); // Generate a sample document using the font collection to provide fonts: var doc = new GcPdfDocument(); var page = doc.Pages.Add(); var g = page.Graphics; // For TextLayout/TextFormat to be able to use a font collection, it must be // associated with it like so: var tl = new TextLayout(g.Resolution) { FontCollection = fc }; // Render some strings using the different fonts from our collection: var tf = new TextFormat() { FontName = "times new roman", FontSize = 16 }; tl.Append("Using FontCollection to manage fonts and render text\n\n", tf); tf.FontSize = 12; tl.Append("Text rendered using Times New Roman regular font. \n", tf); // Setting a font style (bold or italic) will tell the font collection // to search for a suitable font (if it is not found, emulation will be used): tf.FontItalic = true; tl.Append("Text rendered using Times New Roman italic font. \n", tf); // Text format is applied to a text run when the text is appended, // so we can re-use the same format, modifying its properties // to render differently formatted texts: tf.FontBold = true; tl.Append("Text rendered using Times New Roman bold italic font. \n", tf); tf.FontItalic = false; tl.Append("Text rendered using Times New Roman bold font. \n", tf); tf.FontName = "segoe ui"; tl.Append("Text rendered using Segoe UI bold font. \n", tf); tf.FontBold = false; tl.Append("Text rendered using Segoe UI regular font. \n", tf); // Apply page settings to the page layout and render the page: tl.MaxWidth = page.Size.Width; tl.MaxHeight = page.Size.Height; tl.MarginAll = 72; tl.PerformLayout(true); g.DrawTextLayout(tl, PointF.Empty); // If using GcGraphics.DrawString/MeasureString, this will allow the TextLayout // created internally by GcGraphics to find the specified fonts in the font collection: g.FontCollection = fc; // Use GcGraphics.DrawString to show that the font collection is also used // by the graphics once the FontCollection has been set on it: g.DrawString("Text rendered using Segoe UI bold, drawn by GcGraphics.DrawString() method.", new TextFormat() { FontName = "segoe ui", FontBold = true, FontSize = 10 }, new PointF(72, tl.ContentRectangle.Bottom + 12)); // Done: doc.Save(stream); }