I am using PDFsharp to create PDF documents. XTextFormatter is being used to wrap the text if it is going beyond specified y co-ordinate. However, I want to place "Phone number" just after the "tf.DrawString" text ends.
Currently, texts are getting overlapped as attached see here.
var dy = 13;
graphics.DrawString("Delivery Location: ", font, XBrushes.Black, new XPoint(20, 111));
graphics.DrawString(comp_name, font, XBrushes.Black, new XPoint(20, 111+ dy));
XRect rect = new XRect(20, 124, 200, 34);
graphics.DrawRectangle(XBrushes.SeaShell, rect);
tf.DrawString("This is just test. This is just test. This is just test. This is just test.",
font,
XBrushes.Black,
new XRect(rect.X + 5, rect.Y, rect.Width - 5, 34), XStringFormats.TopLeft);
graphics.DrawString("Phone Number: " + comp_phone1, font, XBrushes.Black, new XPoint(20, 136+ dy));
graphics.DrawString("GST: " + comp_gst, font, XBrushes.Black, new XPoint(20, 149+ dy));
graphics.DrawString("PAN: " + comp_pan, font, XBrushes.Black, new XPoint(20, 159+ dy));
The XTextFormatter is just a demo to get you started. Upgrade it according to your needs.
An upgraded version can be found here:
https://forum.pdfsharp.net/viewtopic.php?f=8&t=3192
This version returns the height needed for the text. You can add more text below.
Related
I draw a string into a larger graphic in C# using DrawString:
g.DrawString("Re", new Font("PT Sans Narrow", 35, FontStyle.Bold), Brushes.Black, new Point(5, 0));
Now I want to draw the text in Small Caps as it is possible in many word processors or graphic tools ... and I don't mean the faked small caps like "Draw 1st letter larger than rest".
Is this possible in C# / .NET?
Why not just render capitalized string?
If you need strings perfectly fit to echa other, you can measure size of rendered string, like this:
string s1 = "Simple text";
string s2 = "Capitalized text";
Font font1 = new Font("PT Sans Narrow", 35, FontStyle.Bold);
Font font2 = new Font("PT Sans Narrow", 25, FontStyle.Bold);
SizeF size1 = g.MeasureString(s1, font1);
SizeF size2 = g.MeasureString(s2, font2);
Point point1 = new Point(5, 0);
Point point2 = new Point(point1.X + size1.Width, point1.Y + size1.Height - size2.Height);
g.DrawString(s1, font1, Brushes.Black, point1);
g.DrawString(s2.ToUpper(), font2, Brushes.Black, point2);
font1.Dispose();
font2.Dispose();
How about something like:
string output = "Re";
g.DrawString(output.ToLower(), new Font("PT Sans Narrow", 35, FontStyle.Bold), Brushes.Black, new Point(5, 0));
Code below is used to print order.
Order can have variable number of lines written out by first DrawString call.
Text
Bottom line1
Bottom line2
must appear in bottom left corner in page.
Code below uses hard coded value e.MarginBounds.Top + 460 to print this.
How to remove this hard-coded value so that text is printed in bottom of page ?
var doc = new PrintDocument();
doc.PrinterSettings.PrinterName = "PDFCreator";
doc.PrintPage += new PrintPageEventHandler(ProvideContent);
doc.Print();
void ProvideContent(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString("string containing variable number of lines", new Font("Arial", 12),
Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top);
e.Graphics.DrawString("Bottom line1\r\nBottom line2", new Font("Courier", 10), Brushes.Black,
e.MarginBounds.Left, e.MarginBounds.Top + 460);
}
Measure your string, then move up that height from the bottom?
void ProvideContent(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString("string containing variable number of lines", new Font("Arial", 12),
Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top);
string bottom = "Bottom line1\r\nBottom line2";
Font courier = new Font("Courier", 10);
Size sz = TextRenderer.MeasureText(bottom, courier);
e.Graphics.DrawString(bottom, courier, Brushes.Black,
e.MarginBounds.Left, e.MarginBounds.Bottom - sz.Height);
}
how i can edit the text of a drawing rectangle (class digram) when i double click on it ? this the code in c# how i draw the class digram
g1.DrawRectangle(pen, x1, y1, width, height);
g1.DrawString("class Name", new Font("Arial", 10), new SolidBrush(Color.Black), x1,(y1 +5)); //TextRenderingHint
g1.DrawLine(pen, x1, y1 + 23, x1 + width, y1 + 23);
g1.DrawString("+ attribute :type ", new Font("Arial", 10), new SolidBrush(Color.Black), x1, (y1 + 30));
g1.DrawLine(pen, x1, y1 + 48, x1 + width, y1 + 48);
g1.DrawString("+ operation (args list):return", new Font("Arial", 10), new SolidBrush(Color.Black), x1, (y1 + 55));
Well, you can't.
You are not working with variables, but you are rendering an output on the screen -> you painted something. You would need to locate the area, erase it and redraw your string.
If you want to edit your content, you would need to integrate controls like Textboxes or labels into your diagram.
hi im using PDFSharp for creating PDF-Document for some diagrams. after converting my diagrams in PDF, i should print them on one Page for very small Diagrams, but if i have big-Diagrams then printing them on one Page produce a bad Printing quality the diagram will be small displayed and the diagram content is not readable. if i give a high Scale, the diagram will be larger displayed but some of the nodes will disapear.
so how can i create more pages that depend on my Scale and Diagram-Size?
private void convertBitmap(BitmapSource Img)
{
try
{
PdfSharp.Pdf.PdfDocument document = new PdfSharp.Pdf.PdfDocument();
document.Info.Title = activeDiagram.Model.Name;
PdfSharp.Pdf.PdfPage pdfPage = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(pdfPage);
XImage xIMage = XImage.FromBitmapSource(Img);
XImage logo = XImage.FromFile("logo.png");
pdfPage.Width = xIMage.PointWidth;
pdfPage.Height = xIMage.PointHeight;
//draw the logo
gfx.DrawImage(xIMage, 15, 70, pdfPage.Width, pdfPage.Height);
gfx.DrawImage(logo, 500, 5);
// Draw the texts
string typ = "";
if (activeDiagram == myDiagram1)
typ = "EPC";
XFont font = new XFont("Arial", 12, XFontStyle.Bold);
XFont font2 = new XFont("Arial", 10, XFontStyle.Bold);
gfx.DrawString("Modelname: " + activeDiagram.Model.Name, font, XBrushes.Black,
new XRect(50, 5, 400, 20), XStringFormats.TopLeft);
gfx.DrawString("Modeltyp: " + typ, font, XBrushes.Black, new XRect(50, 25, 400,
20), XStringFormats.TopLeft);
gfx.DrawLine(new XPen(XColor.FromKnownColor(XKnownColor.CornflowerBlue), 2), 20,
45, 600, 45);
gfx.DrawLine(new XPen(XColor.FromKnownColor(XKnownColor.CornflowerBlue), 2), 20,
900, 600, 900);
gfx.DrawString("Date: " + DateTime.Now.ToShortDateString(), font2, XBrushes.Black,
new XRect(50, 905, 100, 25), XStringFormats.TopLeft);
gfx.DrawString("Page: 1 von 1 ", font2, XBrushes.Black, new XRect(530, 905, 100,
25), XStringFormats.TopLeft);
SaveFileDialog dlg = new SaveFileDialog();
lg.FileName = activeDiagram.Model.Name;
dlg.AddExtension = true;
dlg.DefaultExt = "pdf";
dlg.Filter = "PDF Document|*.pdf|*.pdf|";
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
// Save the document...
string filename = dlg.FileName;
document.Save(filename);
// ...and start a viewer.
Process.Start(filename);
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message, "Error saving graph as a
pdf");
}
}
Creating multiple pages with PDFsharp is simple - but PDFsharp is not prepared to distribute your bitmap accross several pages, so this task is left to you.
Depending on the size of the bitmap, your code should decide to cut the image into two or four parts and draw them on two or four pages. This way you don't have to rely onto the capabilities of a printer driver.
PDFsharp can create larger pages - but then you'd have to rely onto the capabilities of the printer driver to print a single PDF page onto several physical pages. That may or may not work.
If you split the image yourself, you have full control of the PDF file that comes out. I'd recommend having the two or four segments printed with a common (overlapping) strip.
Hi there
i had been sucessfully using this great library PDF Sharp.now i wanted to play with some dynamic Stuff so people recomended to switch to Migradoc I did and like its paragraph feature.Now the problem is that when i add long paragraph then new page is not added instead of that there is incomplete text shown(incomplete in sense that text overflows) and i have added a image at the bottom for footer look.how can i do that i enter dynamic text (variable length ) and it just adds the required Number of pages.
My code so far is
XFont font = new XFont("Times New Roman", 12, XFontStyle.Bold);
XFont fontReg = new XFont("Times New Roman", 12, XFontStyle.Regular);
// HACKĀ²
gfx.MUH = PdfFontEncoding.Unicode;
gfx.MFEH = PdfFontEmbedding.Default;
string appPath = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
XImage image = XImage.FromFile(appPath + "/header.png");
gfx.DrawImage(image, 0, 0);
//Civil Stamp
gfx.DrawImage(XImage.FromFile(appPath + "/cStamp.png"), 370, 380);
gfx.DrawImage(XImage.FromFile(appPath + "/Sp.png"), 230, 380);
CoverPageHeader();
Document doc = new Document();
MigraDoc.DocumentObjectModel.Section sec = doc.AddSection();
// Add a single paragraph with some text and format information.
MigraDoc.DocumentObjectModel.Paragraph para = sec.AddParagraph();
para.Format.Alignment = ParagraphAlignment.Left;
para.Format.Font.Name = "Times New Roman";
para.Format.Font.Size = 12;
para.Format.Font.Color = MigraDoc.DocumentObjectModel.Colors.Black;
para.AddText("We are pleased to present the attached Document Please review the Agreement and, if acceptable, " +
"sign one copy and return it to us. We will sign the copy of the agreement and return one for " +
"your records.");
para.AddLineBreak();
para.AddLineBreak();
para.AddText(longlongtextString);
para.AddLineBreak();
para.AddLineBreak();
para.AddText("Sincerely,");
MigraDoc.Rendering.DocumentRenderer docRenderer = new DocumentRenderer(doc);
docRenderer.PrepareDocument();
// Render the paragraph. You can render tables or shapes the same way.
docRenderer.RenderObject(gfx, XUnit.FromCentimeter(0.7), XUnit.FromCentimeter(9), "18cm", para);
gfx.DrawString("Kelly Turbin PhD., P.E.-SECB", font, XBrushes.Black, 20, 500);
gfx.DrawString("Principal", font, XBrushes.Black, 20, 520);
gfx.DrawString("Project No " + textBoxProjNumber.Text, fontReg, XBrushes.Black, 20,785);
gfx.DrawImage(XImage.FromFile(appPath + "/AccB.png"), 20, 700);
gfx.DrawImage(XImage.FromFile(appPath + "/ScreenMagic.png"), 100, 690);
gfx.DrawImage(XImage.FromFile(appPath + "/Footer.png"), 220, 750);
}
Don't use RenderObject, instead use RenderDocument and pages will be created automatically as needed.
Sample code here:
http://www.pdfsharp.net/wiki/HelloMigraDoc-sample.ashx
There is no way to automatically create new pages based on text size, when you are using the Mix of PDFSharp and MigraDoc, as it is in the question. The only solution is to use only MigraDoc with its RenderDocument method.