static void Main(string[] args) { // Create a new PDF document PdfDocument document = new PdfDocument(); // Create an empty page PdfPage page = document.AddPage(); //page.Contents.CreateSingleContent().Stream.UnfilteredValue; // Get an XGraphics object for drawing XGraphics gfx = XGraphics.FromPdfPage(page); XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always); // Create a font XFont font = new XFont("Arial", 20, XFontStyle.Bold, options); // Draw the text gfx.DrawString("Hello, World!", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center); // Save the document... string filename = "HelloWorld.pdf"; document.Save(filename); // ...and start a viewer. Process.Start(filename); }
public BarCodeRenderInfo(XGraphics gfx, XBrush brush, XFont font, XPoint position) { Gfx = gfx; Brush = brush; Font = font; Position = position; }
/// <summary> /// Initializes a new instance of PdfTrueTypeFont from an XFont. /// </summary> public PdfTrueTypeFont(PdfDocument document, XFont font) : base(document) { Elements.SetName(Keys.Type, "/Font"); Elements.SetName(Keys.Subtype, "/TrueType"); // TrueType with WinAnsiEncoding only. OpenTypeDescriptor ttDescriptor = (OpenTypeDescriptor)FontDescriptorCache.GetOrCreateDescriptorFor(font); FontDescriptor = new PdfFontDescriptor(document, ttDescriptor); _fontOptions = font.PdfOptions; Debug.Assert(_fontOptions != null); //cmapInfo = new CMapInfo(null/*ttDescriptor*/); _cmapInfo = new CMapInfo(ttDescriptor); BaseFont = font.GlyphTypeface.GetBaseName(); if (_fontOptions.FontEmbedding == PdfFontEmbedding.Always) BaseFont = PdfFont.CreateEmbeddedFontSubsetName(BaseFont); FontDescriptor.FontName = BaseFont; Debug.Assert(_fontOptions.FontEncoding == PdfFontEncoding.WinAnsi); if (!IsSymbolFont) Encoding = "/WinAnsiEncoding"; Owner._irefTable.Add(FontDescriptor); Elements[Keys.FontDescriptor] = FontDescriptor.Reference; FontEncoding = font.PdfOptions.FontEncoding; }
public override void RenderPage(XGraphics gfx) { base.RenderPage(gfx); string facename = "Times"; XFont fontR = new XFont(facename, 40); XFont fontB = new XFont(facename, 40, XFontStyle.Bold); XFont fontI = new XFont(facename, 40, XFontStyle.Italic); XFont fontBI = new XFont(facename, 40, XFontStyle.Bold | XFontStyle.Italic); //gfx.DrawString("Hello", this.properties.Font1.Font, this.properties.Font1.Brush, 200, 200); double x = 80; XPen pen = XPens.SlateBlue; gfx.DrawLine(pen, x, 100, x, 600); gfx.DrawLine(pen, x - 50, 200, 400, 200); gfx.DrawLine(pen, x - 50, 300, 400, 300); gfx.DrawLine(pen, x - 50, 400, 400, 400); gfx.DrawLine(pen, x - 50, 500, 400, 500); double lineSpace = fontR.GetHeight(gfx); int cellSpace = fontR.FontFamily.GetLineSpacing(fontR.Style); int cellAscent = fontR.FontFamily.GetCellAscent(fontR.Style); int cellDescent = fontR.FontFamily.GetCellDescent(fontR.Style); double cyAscent = lineSpace * cellAscent / cellSpace; XFontMetrics metrics = fontR.Metrics; XSize size; gfx.DrawString("Times 40", fontR, this.properties.Font1.Brush, x, 200); size = gfx.MeasureString("Times 40", fontR); gfx.DrawLine(this.properties.Pen3.Pen, x, 200, x + size.Width, 200); gfx.DrawString("Times bold 40", fontB, this.properties.Font1.Brush, x, 300); size = gfx.MeasureString("Times bold 40", fontB); //gfx.DrawLine(this.properties.Pen3.Pen, x, 300, x + size.Width, 300); gfx.DrawString("Times italic 40", fontI, this.properties.Font1.Brush, x, 400); size = gfx.MeasureString("Times italic 40", fontI); //gfx.DrawLine(this.properties.Pen3.Pen, x, 400, x + size.Width, 400); gfx.DrawString("Times bold italic 40", fontBI, this.properties.Font1.Brush, x, 500); size = gfx.MeasureString("Times bold italic 40", fontBI); //gfx.DrawLine(this.properties.Pen3.Pen, x, 500, x + size.Width, 500); #if true___ // Check Malayalam XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always); XFont Kartika = new XFont("Kartika", 20, XFontStyle.Regular, options); XFont AnjaliOldLipi = new XFont("AnjaliOldLipi", 20, XFontStyle.Regular, options); gfx.DrawString("മകനെ ഇത് ഇന്ത്യയുടെ ഭൂപടം", Kartika, this.properties.Font1.Brush, x, 600); gfx.DrawString("മകനെ ഇത് ഇന്ത്യയുടെ ഭൂപടം", AnjaliOldLipi, this.properties.Font1.Brush, x, 650); #endif }
private static void FixTableSize(Table table) { XGraphics graphics = XGraphics.CreateMeasureContext(new XSize(2000, 2000), XGraphicsUnit.Point, XPageDirection.Downwards); var fontSize = table.Document.Styles["Table"].Font.Size.Value; var gdiFont = new PdfSharp.Drawing.XFont("Arial", fontSize); for (int j = 0; j < table.Columns.Count; j++) { double columnWidth = 0; for (int i = 0; i < table.Rows.Count; i++) { foreach (Paragraph paragraph in table[i, j].Elements.OfType <Paragraph>()) { string contents = string.Empty; foreach (DocumentObject paragraphElement in paragraph.Elements) { if (paragraphElement is MigraDoc.DocumentObjectModel.Text) { contents += (paragraphElement as MigraDoc.DocumentObjectModel.Text).Content; } else if (paragraphElement is MigraDoc.DocumentObjectModel.Hyperlink) { contents += (paragraphElement as MigraDoc.DocumentObjectModel.Hyperlink).Name; } } XSize size = graphics.MeasureString(contents, gdiFont); columnWidth = Math.Max(columnWidth, size.Width); } } table.Columns[j].Width = Unit.FromPoint(columnWidth) + 5; } }
public void HelloWord() { document.Info.Title = "Created with PDFsharp"; // Create an empty page PdfPage page = document.AddPage(); // Get an XGraphics object for drawing XGraphics gfx = XGraphics.FromPdfPage(page); //XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always); // Create a font XFont font = new XFont("Times New Roman", 20, XFontStyle.BoldItalic); // Draw the text gfx.DrawString("Hello, World!", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center); // Save the document... const string filename = "HelloWorld_tempfile.pdf"; document.Save(filename); // ...and start a viewer. Process.Start(filename); }
// ...took a look at the source code of WPF. About 50 classes and several 10,000 lines of code // deal with that what colloquial is called 'fonts'. // So let's start simple. /// <summary> /// Simple measure string function. /// </summary> public static XSize MeasureString(string text, XFont font, XStringFormat stringFormat) { XSize size = new XSize(); OpenTypeDescriptor descriptor = FontDescriptorStock.Global.CreateDescriptor(font) as OpenTypeDescriptor; if (descriptor != null) { size.Height = (descriptor.Ascender + Math.Abs(descriptor.Descender)) * font.Size / font.unitsPerEm; Debug.Assert(descriptor.Ascender > 0); bool symbol = descriptor.fontData.cmap.symbol; int length = text.Length; int width = 0; for (int idx = 0; idx < length; idx++) { char ch = text[idx]; int glyphIndex = 0; if (symbol) { glyphIndex = ch + (descriptor.fontData.os2.usFirstCharIndex & 0xFF00); // @@@ glyphIndex = descriptor.CharCodeToGlyphIndex((char)glyphIndex); } else glyphIndex = descriptor.CharCodeToGlyphIndex(ch); //double width = descriptor.GlyphIndexToEmfWidth(glyphIndex, font.Size); //size.Width += width; width += descriptor.GlyphIndexToWidth(glyphIndex); } size.Width = width * font.Size * (font.Italic ? 1 : 1) / descriptor.UnitsPerEm; } Debug.Assert(descriptor != null, "No OpenTypeDescriptor."); return size; }
static void Main() { // Create new document PdfDocument document = new PdfDocument(); // Set font encoding to unicode XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always); XFont font = new XFont("Times New Roman", 12, XFontStyle.Regular, options); // Draw text in different languages for (int idx = 0; idx < texts.Length; idx++) { PdfPage page = document.AddPage(); XGraphics gfx = XGraphics.FromPdfPage(page); XTextFormatter tf = new XTextFormatter(gfx); tf.Alignment = XParagraphAlignment.Left; tf.DrawString(texts[idx], font, XBrushes.Black, new XRect(100, 100, page.Width - 200, 600), XStringFormats.TopLeft); } const string filename = "Unicode_tempfile.pdf"; // Save the document... document.Save(filename); // ...and start a viewer. Process.Start(filename); }
public TableCell(string text, XFont font, int columnSpan, HorizontalAlignment alignment) { Text = text; Font = font; Alignment = alignment; ColumnSpan = columnSpan; }
public override void RenderPage(XGraphics gfx) { //base.RenderPage(gfx); XFont font1 = new XFont("Arial", 9); XFont font2 = new XFont("Arial", 9, XFontStyle.Italic); XFont font3 = new XFont("Arial", 9, XFontStyle.Bold); XSolidBrush brush = new XSolidBrush(XColors.Black); gfx.DrawString("Page 5", font1, brush, 100, 100); gfx.DrawString("Water Sports You've Done continued", font2, brush, 100, 200); gfx.DrawString("Rowing", font1, brush, 100, 250); gfx.DrawString("Snorkeling", font1, brush, 100, 300); gfx.DrawString("WINTER SPORTS, SKIING, SKIING IN THE US", font3, XBrushes.Red, 100, 320); gfx.DrawLine(XPens.Red, 100, 100, 200, 200); gfx.DrawString("Resorts You've Skied in Vermont", font3, brush, 100, 400); gfx.DrawString("Haystack", font1, brush, 100, 420); // string text = "TgfÄÖÜWi9"; // if (this.properties.Font1.Text != "") // text = this.properties.Font1.Text; // float x = 100, y = 300; // string familyName = properties.Font1.FamilyName; // XFontStyle style = this.properties.Font1.Style; // float emSize = this.properties.Font1.Size; // XFont font = CreateFont(familyName, emSize, style); // font = this.properties.Font1.Font; // XSize size = gfx.MeasureString(text, font); // double lineSpace = font.GetHeight(gfx); // int cellSpace = font.FontFamily.GetLineSpacing(style); // int cellAscent = font.FontFamily.GetCellAscent(style); // int cellDescent = font.FontFamily.GetCellDescent(style); // int cellLeading = cellSpace - cellAscent - cellDescent; // double ascent = lineSpace * cellAscent / cellSpace; // gfx.DrawRectangle(XBrushes.Bisque, x, y - ascent, size.Width, ascent); // double descent = lineSpace * cellDescent / cellSpace; // gfx.DrawRectangle(XBrushes.LightGreen, x, y, size.Width, descent); // double leading = lineSpace * cellLeading / cellSpace; // gfx.DrawRectangle(XBrushes.Yellow, x, y + descent, size.Width, leading); // //gfx.DrawRectangle(this.properties.Brush1.Brush, x, y - size.Height, size.Width, size.Height); // //gfx.DrawLine(this.properties.Pen2.Pen, x, y, x + size.Width, y); // //gfx.DrawString("Hello", this.properties.Font1.Font, this.properties.Font1.Brush, 200, 200); //#if true_ // XPdfFontOptions pdfOptions = new XPdfFontOptions(false, true); // font = new XFont("Tahoma", 8, XFontStyle.Regular, pdfOptions); // text = "Hallo"; // text = chinese; //#endif // gfx.DrawString(text, font, this.properties.Font1.Brush, x, y); // gfx.DrawLine(XPens.Red, 0, 0, 100, 100); // gfx.DrawString(text, font, this.properties.Font1.Brush, x, y + 20); }
public void InitializeFonts(string font) { //Regular Font8 = new XFont(font, 8, XFontStyle.Regular); Font10 = new XFont(font, 10, XFontStyle.Regular); Font12 = new XFont(font, 12, XFontStyle.Regular); Font14 = new XFont(font, 14, XFontStyle.Regular); Font16 = new XFont(font, 16, XFontStyle.Regular); Font18 = new XFont(font, 18, XFontStyle.Regular); //Bold Font8Bold = new XFont(font, 8, XFontStyle.Bold); Font10Bold = new XFont(font, 10, XFontStyle.Bold); Font12Bold = new XFont(font, 12, XFontStyle.Bold); Font14Bold = new XFont(font, 14, XFontStyle.Bold); Font16Bold = new XFont(font, 16, XFontStyle.Bold); Font18Bold = new XFont(font, 18, XFontStyle.Bold); //Italic Font8Italic = new XFont(font, 8, XFontStyle.Italic); Font10Italic = new XFont(font, 10, XFontStyle.Italic); Font12Italic = new XFont(font, 12, XFontStyle.Italic); Font14Italic = new XFont(font, 14, XFontStyle.Italic); Font16Italic = new XFont(font, 16, XFontStyle.Italic); Font18Italic = new XFont(font, 18, XFontStyle.Italic); }
private static void CreatePdfPage(List<JiraTicket> issues, ref PdfDocument pdf) { PdfPage page = pdf.AddPage(); page.Size = PdfSharp.PageSize.A4; page.Orientation = PdfSharp.PageOrientation.Landscape; for (int j = 0; j < issues.Count; j++) { string text = issues[j].fields.issuetype.name + System.Environment.NewLine + issues[j].key + System.Environment.NewLine + issues[j].fields.summary + System.Environment.NewLine + issues[j].fields.customfield_10008; XGraphics gfx = XGraphics.FromPdfPage(page); XFont font = new XFont("Verdana", 20, XFontStyle.Bold); XTextFormatter tf = new XTextFormatter(gfx); XRect rect = new XRect(); if (j < 3) { rect = new XRect(15, 15 + j * 180, 400, 170); } else { rect = new XRect(430, 15 + (j - 3) * 180, 400, 170); } gfx.DrawRectangle(XBrushes.SeaShell, rect); tf.DrawString(text, font, XBrushes.Black, rect, XStringFormats.TopLeft); gfx.Dispose(); } }
public StringObject(XGraphics graphics, string text, XFont font) { this.graphics = graphics; Text = text; Font = font; Brush = Brushes.Black; }
internal static XUnit GetDescent(XFont font) { XUnit descent = font.Metrics.Descent; descent *= font.Size; descent /= font.FontFamily.GetEmHeight(font.Style); return descent; }
static void Main() { // Create a new PDF document PdfDocument document = new PdfDocument(); // Create a font XFont font = new XFont("Verdana", 16); // Create first page PdfPage page = document.AddPage(); XGraphics gfx = XGraphics.FromPdfPage(page); gfx.DrawString("Page 1", font, XBrushes.Black, 20, 50, XStringFormats.Default); // Create the root bookmark. You can set the style and the color. PdfOutline outline = document.Outlines.Add("Root", page, true, PdfOutlineStyle.Bold, XColors.Red); // Create some more pages for (int idx = 2; idx <= 5; idx++) { page = document.AddPage(); gfx = XGraphics.FromPdfPage(page); string text = "Page " + idx; gfx.DrawString(text, font, XBrushes.Black, 20, 50, XStringFormats.Default); // Create a sub bookmark outline.Outlines.Add(text, page, true); } // Save the document... const string filename = "Bookmarks_tempfile.pdf"; document.Save(filename); // ...and start a viewer. Process.Start(filename); }
public override void RenderPage(XGraphics gfx) { //base.RenderPage(gfx); XFont font1 = new XFont("Times New Roman", 12); XFont font2 = new XFont("Courier New", 10, XFontStyle.Bold); gfx.WriteComment("Word 11"); gfx.DrawString("Word 11", font1, XBrushes.Black, new XPoint(50, 100)); gfx.WriteComment("Word 12"); gfx.DrawString("Word 12", font1, XBrushes.Black, new XPoint(100, 100)); gfx.WriteComment("Word 21"); gfx.DrawString("Word 21", font2, XBrushes.Black, new XPoint(50, 200)); gfx.WriteComment("Word 22"); gfx.DrawString("Word 22", font2, XBrushes.Black, new XPoint(100, 200)); gfx.WriteComment("Word 23"); gfx.DrawString("Word 22", font2, XBrushes.Red, new XPoint(150, 200)); gfx.WriteComment("Word 24"); gfx.DrawString("Word 24", font2, XBrushes.Red, new XPoint(200, 200)); }
public Export_to_Pdf() { InitializeComponent(); //Create object for pdf. document = new PdfDocument(); page = document.AddPage(); gfx = XGraphics.FromPdfPage(page); //Set by default values. font = new XFont("Arial", 12, XFontStyle.Regular); header_font = new XFont("Arial", 12, XFontStyle.Regular); page.Size = PageSize.A4; page.Orientation = PageOrientation.Portrait; ////////////////////////////////////////////////////////////////////// //Create a fake questionanire to test the print process to pdf. if (questionaire != null) { List<Question> question_list = new List<Question>(); List<Answer> answer_list = new List<Answer>(); Answer ans = new Answer(); ans.Answer_descr = "einai to dsfgdsfgsd"; answer_list.Add(ans); answer_list.Add(ans); answer_list.Add(ans); Question quest = new Question() { AnswerList = answer_list }; quest.Question_descr = "H ekfoonisi tis erotisis aytis einai blablbalbablbalblab"; for (int i = 0; i < 10; i++) { question_list.Add(quest); } questionaire = new Quastionnaire.Model.Questionaire() { Questionaire_descr = "sdfsakdfjdflshsadflkjashflasdfkh", QuestionList = question_list }; } ////////////////////////////////////////////////////////////////////// }
public Uri GetCookBook(string name) { var document = new PdfDocument(); document.Info.Title = name + "'s personalized cookbook"; document.Info.Author = "Pancake Prowler"; var page = document.AddPage(); var graphics = XGraphics.FromPdfPage(page); var font = new XFont("Verdana", 20, XFontStyle.BoldItalic); graphics.DrawString(name + "'s personalized cookbook", font, XBrushes.Red, new System.Drawing.PointF((float)page.Width / 2, (float)page.Height / 2), XStringFormats.Center); var saveStream = new MemoryStream(); document.Save(saveStream); var memoryStream = new MemoryStream(saveStream.ToArray()); memoryStream.Seek(0, SeekOrigin.Begin); var imageStore = new BlobImageRepository(); return imageStore.Save("application/pdf", memoryStream, "cookbooks"); }
void RenderMeasureText(XGraphics gfx) { gfx.TranslateTransform(15, 20); XFontStyle style = XFontStyle.Regular; XFont font = new XFont("Times New Roman", 95, style); string text = "Hello"; double x = 20, y = 100; XSize size = gfx.MeasureString(text, font); double lineSpace = font.GetHeight(gfx); int cellSpace = font.FontFamily.GetLineSpacing(style); int cellAscent = font.FontFamily.GetCellAscent(style); int cellDescent = font.FontFamily.GetCellDescent(style); int cellLeading = cellSpace - cellAscent - cellDescent; // Get effective ascent double ascent = lineSpace * cellAscent / cellSpace; gfx.DrawRectangle(XBrushes.Bisque, x, y - ascent, size.Width, ascent); // Get effective descent double descent = lineSpace * cellDescent / cellSpace; gfx.DrawRectangle(XBrushes.LightGreen, x, y, size.Width, descent); // Get effective leading double leading = lineSpace * cellLeading / cellSpace; gfx.DrawRectangle(XBrushes.Yellow, x, y + descent, size.Width, leading); // Draw text half transparent XColor color = XColors.DarkSlateBlue; color.A = 0.6; gfx.DrawString(text, font, new XSolidBrush(color), x, y); }
public Cell(string text, XUnit width, XFont font, Alignment alignment) { this.Text = text; this.width = width; this.font = font; this.alignment = alignment; this.height = XUnit.Zero; }
public SignObject(XGraphics graphics, string subjectText, string signatureText, string dateText, XFont font) { this.graphics = graphics; SubjectText = subjectText; DateText = dateText; SignatureText = signatureText; Font = font; Brush = Brushes.Black; }
public void AppendHeader(TNaglowek naglowek, string nip) { var headerBigFont = new XFont(DefaultValues.Groupbox.Font.FontFamily.Name, 12, XFontStyle.Bold, DefaultValues.Groupbox.Font.PdfOptions); var nipContent = new GroupBox(0, _document.Width * 3 / 4, "") { MarginLeft = 0, MarginTop = 0, MarginRight = 0, MarginBottom = 0 }; var percentRowWidths = new[] { 45, 35, 20 }; var rowControls = new[] { new InputBox(Resources.InputTitle1, nip), new InputBox(Resources.InputTitle2) {Brush = XBrushes.LightGray}, new InputBox(Resources.InputTitle3) {Brush = XBrushes.LightGray}, }; nipContent.AddRow(rowControls, percentRowWidths); _document.AddContainer(nipContent); var miesiacVal = naglowek.DateType == ItemChoiceType.Miesiac ? naglowek.Miesiac.ToString() : ""; var kwartalVal = naglowek.ItemElementName == ItemChoiceType.Kwartal ? naglowek.Kwartal.ToString() : ""; var titleContent = new GroupBox(0, _document.Width, "") { MarginBottom = 10, Brush = new XSolidBrush(XColor.FromArgb(128, 128, 255)), Controls = new List<IControl> { new Label(20, 0, Resources.TitleLabel1) {Font = headerBigFont}, new Label(150, 0, Resources.TitleLabel2) {Font = headerBigFont}, new Label(270, 50, Resources.TitleLabel3) {Font = headerBigFont}, new InputBox(new XRect(200, 40, 60, 30), Resources.InputTitle4, miesiacVal), new InputBox(new XRect(300, 40, 60, 30), Resources.InputTitle5, kwartalVal), new InputBox(new XRect(400, 40, 80, 30), Resources.InputTitle6, naglowek.Rok) } }; _document.AddContainer(titleContent); var infoFont = new XFont(DocumentContainer.FontFamily, 8); var infoContent = new GroupBox(0, _document.Width, "") { MarginLeft = 5, MarginTop = 1, MarginRight = 1, MarginBottom = 1, Controls = new List<IControl> { new Label(0, 0, Resources.InfoContent0Label) {Font = infoFont}, new Label(0, 10, Resources.InfoContent1Label) {Font = infoFont}, new Label(0, 40, Resources.InfoContent2Label) {Font = infoFont}, new Label(0, 50, Resources.InfoContent3Label) {Font = infoFont}, new Label(new XRect(80, 0, _document.Width, 50), Resources.InfoContent0Label){Font = infoFont}, new Label(new XRect(80, 10, _document.Width, 50), Resources.InfoContent1Value){Font = infoFont}, new Label(new XRect(80, 40, _document.Width, 50), Resources.InfoContent2Value){Font = infoFont}, new Label(new XRect(80, 50, _document.Width, 70), Resources.InfoContent3Value){Font = infoFont}, } }; _document.AddContainer(infoContent); }
public virtual void Render(XRect rect, XGraphics gfx, string fontName) { //gfx.DrawRectangle(XBrushes.Red, rect); var font = new XFont(fontName, FontSize, XFontStyle.Regular); var brush = new XSolidBrush(ForegroundColor); var textFormatter = new XTextFormatter(gfx); textFormatter.Alignment = XParagraphAlignment.Left; textFormatter.DrawString(Text, font, brush, rect); }
public override void RenderPage(XGraphics gfx) { base.RenderPage(gfx); string text = "TgfÄÖÜWi9"; if (this.properties.Font1.Text != "") text = this.properties.Font1.Text; float x = 100, y = 300; string familyName = properties.Font1.FamilyName; XFontStyle style = this.properties.Font1.Style; float emSize = this.properties.Font1.Size; //familyName = "Verdana"; //style = XFontStyle.Regular; //emSize = 20; //text = "X"; XFont font = CreateFont(familyName, emSize, style); //font = this.properties.Font1.Font; XSize size = gfx.MeasureString(text, font); double lineSpace = font.GetHeight(gfx); int cellSpace = font.FontFamily.GetLineSpacing(style); int cellAscent = font.FontFamily.GetCellAscent(style); int cellDescent = font.FontFamily.GetCellDescent(style); int cellLeading = cellSpace - cellAscent - cellDescent; double ascent = lineSpace * cellAscent / cellSpace; gfx.DrawRectangle(XBrushes.Bisque, x, y - ascent, size.Width, ascent); double descent = lineSpace * cellDescent / cellSpace; gfx.DrawRectangle(XBrushes.LightGreen, x, y, size.Width, descent); double leading = lineSpace * cellLeading / cellSpace; gfx.DrawRectangle(XBrushes.Yellow, x, y + descent, size.Width, leading); //gfx.DrawRectangle(this.properties.Brush1.Brush, x, y - size.Height, size.Width, size.Height); //gfx.DrawLine(this.properties.Pen2.Pen, x, y, x + size.Width, y); //gfx.DrawString("Hello", this.properties.Font1.Font, this.properties.Font1.Brush, 200, 200); #if true_ XPdfFontOptions pdfOptions = new XPdfFontOptions(false, true); font = new XFont("Tahoma", 8, XFontStyle.Regular, pdfOptions); text = "Hallo"; text = chinese; #endif gfx.DrawString(text, font, this.properties.Font1.Brush, x, y); #if true XFont font2 = CreateFont(familyName, emSize, XFontStyle.Italic); gfx.DrawString(text, font2, this.properties.Font1.Brush, x, y+50); #endif //gfx.DrawLine(XPens.Red, x, y + 10, x + 13.7, y + 10); //gfx.DrawString(text, font, this.properties.Font1.Brush, x, y + 20); }
public PdfType0Font(PdfDocument document, XFont font, bool vertical) : base(document) { Elements.SetName(Keys.Type, "/Font"); Elements.SetName(Keys.Subtype, "/Type0"); Elements.SetName(Keys.Encoding, vertical ? "/Identity-V" : "/Identity-H"); OpenTypeDescriptor ttDescriptor = (OpenTypeDescriptor)FontDescriptorStock.Global.CreateDescriptor(font); this.fontDescriptor = new PdfFontDescriptor(document, ttDescriptor); this.fontOptions = font.PdfOptions; Debug.Assert(this.fontOptions != null); this.cmapInfo = new CMapInfo(ttDescriptor); this.descendantFont = new PdfCIDFont(document, this.fontDescriptor, font); this.descendantFont.CMapInfo = this.cmapInfo; // Create ToUnicode map this.toUnicode = new PdfToUnicodeMap(document, this.cmapInfo); document.Internals.AddObject(toUnicode); Elements.Add(Keys.ToUnicode, toUnicode); //if (this.fontOptions.BaseFont != "") //{ // BaseFont = this.fontOptions.BaseFont; //} //else { BaseFont = font.Name.Replace(" ", ""); switch (font.Style & (XFontStyle.Bold | XFontStyle.Italic)) { case XFontStyle.Bold: this.BaseFont += ",Bold"; break; case XFontStyle.Italic: this.BaseFont += ",Italic"; break; case XFontStyle.Bold | XFontStyle.Italic: this.BaseFont += ",BoldItalic"; break; } } // CID fonts are always embedded BaseFont = PdfFont.CreateEmbeddedFontSubsetName(BaseFont); this.fontDescriptor.FontName = BaseFont; this.descendantFont.BaseFont = BaseFont; PdfArray descendantFonts = new PdfArray(document); Owner.irefTable.Add(descendantFont); descendantFonts.Elements.Add(descendantFont.Reference); Elements[Keys.DescendantFonts] = descendantFonts; }
public void Draw(XGraphics gfx) { string s = "Testtext"; //gfx.DrawLine(XPens.GreenYellow, 5, 100, 30, 50); //gfx.DrawEllipse(XBrushes.DarkBlue, new XRect(30, 40, 250, 235)); XFont font = new XFont("Arial", 40, XFontStyle.Italic); gfx.DrawString(s, font, XBrushes.Firebrick, 40, 60); XSize size = gfx.MeasureString(s, font); gfx.DrawLine(XPens.DarkBlue, 40, 60, 40 + size.Width, 60); gfx.DrawLine(XPens.DarkBlue, 40, 60, 40, 60 + size.Height); }
///<summary>The pdfSharp version of drawstring. g is used for measurement. scaleToPix scales xObjects to pixels.</summary> public static void DrawStringX(XGraphics xg,Graphics g,double scaleToPix,string str,XFont xfont,XBrush xbrush,XRect xbounds, XStringAlignment sa) { //There are two coordinate systems here: pixels (used by us) and points (used by PdfSharp). //MeasureString and ALL related measurement functions must use pixels. //DrawString is the ONLY function that uses points. //pixels: Rectangle bounds=new Rectangle((int)(scaleToPix*xbounds.Left), (int)(scaleToPix*xbounds.Top), (int)(scaleToPix*xbounds.Width), (int)(scaleToPix*xbounds.Height)); FontStyle fontstyle=FontStyle.Regular; if(xfont.Style==XFontStyle.Bold) { fontstyle=FontStyle.Bold; } //pixels: (except Size is em-size) Font font=new Font(xfont.Name,(float)xfont.Size,fontstyle); //pixels: SizeF fit=new SizeF((float)(bounds.Width-rightPad),(float)(font.Height)); StringFormat format=StringFormat.GenericTypographic; //pixels: float pixelsPerLine=LineSpacingForFont(font.Name) * (float)font.Height; float lineIdx=0; int chars; int lines; //points: RectangleF layoutRectangle; for(int ix=0;ix<str.Length;ix+=chars) { if(bounds.Y+topPad+pixelsPerLine*lineIdx>bounds.Bottom) { break; } //pixels: g.MeasureString(str.Substring(ix),font,fit,format,out chars,out lines); //PdfSharp isn't smart enough to cut off the lower half of a line. //if(bounds.Y+topPad+pixelsPerLine*lineIdx+font.Height > bounds.Bottom) { // layoutH=bounds.Bottom-(bounds.Y+topPad+pixelsPerLine*lineIdx); //} //else { // layoutH=font.Height+2; //} //use points here: float adjustTextDown=10f;//this value was arrived at by trial and error. layoutRectangle=new RectangleF( (float)xbounds.X, //(float)(xbounds.Y+(float)topPad/scaleToPix+(pixelsPerLine/scaleToPix)*lineIdx), (float)(xbounds.Y+adjustTextDown+(pixelsPerLine/scaleToPix)*lineIdx), (float)xbounds.Width+50,//any amount of extra padding here will not cause malfunction 0);//layoutH); XStringFormat sf=XStringFormats.Default; sf.Alignment=sa; //sf.LineAlignment= XLineAlignment.Near; //xg.DrawString(str.Substring(ix,chars),xfont,xbrush,layoutRectangle,sf); xg.DrawString(str.Substring(ix,chars),xfont,xbrush,(double)layoutRectangle.Left,(double)layoutRectangle.Top,sf); lineIdx+=1; } }
public void DrawString_SpecialCharacters() { var doc = new PdfDocument(); doc.Options.NoCompression = true; doc.Options.CompressContentStreams = false; doc.Options.ColorMode = PdfColorMode.Rgb; var page = doc.AddPage(); var g = XGraphics.FromPdfPage(page); var font = new XFont("Arial", 96); g.DrawString("π θ", font, XBrushes.Black, 200, 400); doc.Save(Folder + "DrawString_SpecialCharacters.pdf"); }
private static double TamanhoTexto(string nome, double fontSize) { var pdfDoc = new PdfSharp.Pdf.PdfDocument(); var pdfPage = pdfDoc.AddPage(); var pdfGfx = PdfSharp.Drawing.XGraphics.FromPdfPage(pdfPage); var pdfFont = new PdfSharp.Drawing.XFont("Times New Roman", fontSize); var tamanho = pdfGfx.MeasureString(nome, pdfFont).Width; return(tamanho); //pdfGfx.DrawString("Hello World!", pdfFont, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XPoint(100, 100)); }
protected XFont CreateFont(string familyName, double emSize, XFontStyle style, XPdfFontOptions options) { XFont font; try { font = new XFont(familyName, emSize, style, options); } catch { font = new XFont("Courier", emSize, style, options); } return font; }
string IContentStream.GetFontName(XFont font, out PdfFont pdfFont) { return(GetFontName(font, out pdfFont)); }
public void relatorio(int id, int idFesta) { using (var doc = new PdfSharp.Pdf.PdfDocument()) { BD_FESTA festa = new BD_FESTA(); BD_ITENS itens = new BD_ITENS(); String[] dados = new String[4]; dados = festa.pegarDadosFesta(id, idFesta); var page = doc.AddPage(); var graphics = PdfSharp.Drawing.XGraphics.FromPdfPage(page); var textFormatter = new PdfSharp.Drawing.Layout.XTextFormatter(graphics); XBrush titulos = new XSolidBrush(XColor.FromArgb(22, 93, 180)); var font = new PdfSharp.Drawing.XFont("Comic Sans MS", 20); var font2 = new PdfSharp.Drawing.XFont("Verdana", 12); graphics.DrawRoundedRectangle(PdfSharp.Drawing.XPens.Black, 10, 10, 576, 822, 10, 10); textFormatter.DrawString(dados[0], font, titulos, new PdfSharp.Drawing.XRect(40, 50, page.Width, page.Height)); graphics.DrawImage(PdfSharp.Drawing.XImage.FromFile(@"C:\Users\erick\Desktop\Projeto Interdisciplinar 2º Semestre\LAFIESTA\METODIKU\Resources\tiozao.png"), 530, 30, 50, 50); textFormatter.DrawString("Local da festa: ", font2, titulos, new PdfSharp.Drawing.XRect(40, 100, 150, 70)); textFormatter.DrawString(dados[1], font2, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(132, 100, 150, 70)); textFormatter.DrawString("Total de convidados: ", font2, titulos, new PdfSharp.Drawing.XRect(350, 100, 150, 70)); textFormatter.DrawString(dados[2], font2, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(480, 100, 150, 70)); textFormatter.DrawString("Data da festa: ", font2, titulos, new PdfSharp.Drawing.XRect(40, 120, 150, 70)); textFormatter.DrawString(dados[3], font2, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(132, 120, 250, 70)); textFormatter.DrawString("Comidas: ", font2, titulos, new PdfSharp.Drawing.XRect(40, 160, 150, 70)); textFormatter.DrawString("Número", font2, titulos, new PdfSharp.Drawing.XRect(60, 180, 30, 70)); textFormatter.DrawString("Tipo", font2, titulos, new PdfSharp.Drawing.XRect(170, 180, 100, 70)); textFormatter.DrawString("Nome", font2, titulos, new PdfSharp.Drawing.XRect(310, 180, 100, 70)); textFormatter.DrawString("Unidade", font2, titulos, new PdfSharp.Drawing.XRect(470, 180, 100, 70)); DataTable comidas = new DataTable(); comidas = itens.comidas(idFesta); int y = 205; string item = null; if (comidas != null) { for (int i = 0; i < comidas.Rows.Count; i++) { DataRow dr = comidas.Rows[i]; textFormatter.DrawString(dr[0].ToString(), font2, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(80, y, 550, 70)); textFormatter.DrawString(dr[1].ToString(), font2, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(140, y, 550, 70)); textFormatter.DrawString(dr[2].ToString(), font2, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(270, y, 550, 70)); textFormatter.DrawString(dr[3].ToString(), font2, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(480, y, 550, 70)); y += 20; } } textFormatter.DrawString("Bebidas: ", font2, titulos, new PdfSharp.Drawing.XRect(40, y + 20, 150, 70)); y += 40; textFormatter.DrawString("Número", font2, titulos, new PdfSharp.Drawing.XRect(60, y, 30, 70)); textFormatter.DrawString("Tipo", font2, titulos, new PdfSharp.Drawing.XRect(150, y, 100, 70)); textFormatter.DrawString("Nome", font2, titulos, new PdfSharp.Drawing.XRect(290, y, 100, 70)); textFormatter.DrawString("Unidade", font2, titulos, new PdfSharp.Drawing.XRect(470, y, 100, 70)); DataTable bebidas = new DataTable(); bebidas = itens.bebidas(idFesta); if (bebidas != null) { y += 30; for (int i = 0; i < bebidas.Rows.Count; i++) { DataRow dr = bebidas.Rows[i]; textFormatter.DrawString(dr[0].ToString(), font2, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(80, y, 550, 70)); textFormatter.DrawString(dr[1].ToString(), font2, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(140, y, 550, 70)); textFormatter.DrawString(dr[2].ToString(), font2, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(270, y, 550, 70)); textFormatter.DrawString(dr[3].ToString(), font2, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(480, y, 550, 70)); y += 20; } } textFormatter.DrawString("Utensilios: ", font2, titulos, new PdfSharp.Drawing.XRect(40, y + 20, 150, 70)); y += 40; textFormatter.DrawString("Número", font2, titulos, new PdfSharp.Drawing.XRect(60, y, 30, 70)); textFormatter.DrawString("Tipo", font2, titulos, new PdfSharp.Drawing.XRect(150, y, 100, 70)); textFormatter.DrawString("Nome", font2, titulos, new PdfSharp.Drawing.XRect(290, y, 100, 70)); textFormatter.DrawString("Unidade", font2, titulos, new PdfSharp.Drawing.XRect(470, y, 100, 70)); DataTable utensilios = new DataTable(); utensilios = itens.utensilios(idFesta); if (utensilios != null) { y += 30; for (int i = 0; i < utensilios.Rows.Count; i++) { DataRow dr = utensilios.Rows[i]; textFormatter.DrawString(dr[0].ToString(), font2, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(80, y, 550, 70)); textFormatter.DrawString(dr[1].ToString(), font2, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(140, y, 550, 70)); textFormatter.DrawString(dr[2].ToString(), font2, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(270, y, 550, 70)); textFormatter.DrawString(dr[3].ToString(), font2, PdfSharp.Drawing.XBrushes.Black, new PdfSharp.Drawing.XRect(480, y, 550, 70)); y += 20; } } doc.Save(dados[0] + ".pdf"); System.Diagnostics.Process.Start(dados[0] + ".pdf"); } }
public PdfType0Font(PdfDocument document, XFont font, bool vertical) : base(document) { Elements.SetName(Keys.Type, "/Font"); Elements.SetName(Keys.Subtype, "/Type0"); Elements.SetName(Keys.Encoding, vertical ? "/Identity-V" : "/Identity-H"); OpenTypeDescriptor ttDescriptor = (OpenTypeDescriptor)FontDescriptorCache.GetOrCreateDescriptorFor(font); FontDescriptor = new PdfFontDescriptor(document, ttDescriptor); _fontOptions = font.PdfOptions; Debug.Assert(_fontOptions != null); _cmapInfo = new CMapInfo(ttDescriptor); _descendantFont = new PdfCIDFont(document, FontDescriptor, font); _descendantFont.CMapInfo = _cmapInfo; // Create ToUnicode map _toUnicode = new PdfToUnicodeMap(document, _cmapInfo); document.Internals.AddObject(_toUnicode); Elements.Add(Keys.ToUnicode, _toUnicode); BaseFont = font.GlyphTypeface.GetBaseName(); // CID fonts are always embedded BaseFont = PdfFont.CreateEmbeddedFontSubsetName(BaseFont); FontDescriptor.FontName = BaseFont; _descendantFont.BaseFont = BaseFont; PdfArray descendantFonts = new PdfArray(document); Owner._irefTable.Add(_descendantFont); descendantFonts.Elements.Add(_descendantFont.Reference); Elements[Keys.DescendantFonts] = descendantFonts; }
public static void BeginBox(XGraphics gfx, int number, string title, double borderWidth, double borderHeight, XColor shadowColor, XColor backColor, XColor backColor2, XPen borderPen) { const int dEllipse = 15; XRect rect = new XRect(0, 20, 300, 200); if (number % 2 == 0) rect.X = 300 - 5; rect.Y = 40 + ((number - 1) / 2) * (200 - 5); rect.Inflate(-10, -10); XRect rect2 = rect; rect2.Offset(borderWidth, borderHeight); gfx.DrawRoundedRectangle(new XSolidBrush(shadowColor), rect2, new XSize(dEllipse + 8, dEllipse + 8)); XLinearGradientBrush brush = new XLinearGradientBrush(rect, backColor, backColor2, XLinearGradientMode.Vertical); gfx.DrawRoundedRectangle(borderPen, brush, rect, new XSize(dEllipse, dEllipse)); rect.Inflate(-5, -5); XFont font = new XFont("Verdana", 12, XFontStyle.Regular); gfx.DrawString(title, font, XBrushes.Navy, rect, XStringFormats.TopCenter); rect.Inflate(-10, -5); rect.Y += 20; rect.Height -= 20; state = gfx.Save(); gfx.TranslateTransform(rect.X, rect.Y); }