Since the article, because of which I received an invite has disappeared somewhere, I want to publish it again.
Due to the small number of free libraries for working with PDF in .Net as well as their insufficient coverage in Russian, I want to talk about working with such a wonderful library as
PDFsharp and MigradDoc
Perhaps we will start from the very beginning - the source can be downloaded
here or
here.
Now add links to your project:
using MigraDoc; using PdfSharp; using MigraDoc.DocumentObjectModel; using MigraDoc.Rendering; using PdfSharp.Pdf;
Create a new document
Document document = new Document();
Each MigraDoc document consists of independent sections that can have its own format.
Creating a section:
Section section = document.AddSection();
after such initialization, working with
section - we work with the document part.
Sections have functions for working with information, such as
Add, AddParagraph (), AddImage (), AddTable () , etc., as well as a class for changing the format of the
PageSetup section
itself .
Changing format:
section.PageSetup.PageFormat = PageFormat.5;// section.PageSetup.Orientation = Orientation.Portrait;// section.PageSetup.BottomMargin = 10;// section.PageSetup.TopMargin = 10;//
Adding information occurs either using
Add (Param) , where
Param is a paragraph (Paragraph), picture (Image), table (Table), chart (Chart) or text frame (TextFrame)
Paragraph paragraph =new Paragraph(); section.Add(paragraph);
or by the example of adding a section
Paragraph paragraph = section.AddParagraph();
Moreover, for each paragraph it is also possible to set an individual format (Format class) and style (Style class):
paragraph.Format.Font.Color = Color.FromCmyk(100, 30, 20, 50);
For each paragraph you can add information:
Text text = new Text("text"); paragraph.AddText("text");// paragraph.AddFormattedText("formatted text", styleName);// paragraph.Add(text);// paragraph.AddBookmark("Bookmark");// paragraph.AddChar('c');// paragraph.AddDateField("10.10.2010");// paragraph.AddFootnote("Footnote");// //
The basis we have collected, now it remains to turn it into a full PDF document, for which the PdfDocumentRenderer is used:
pdfRenderer = new PdfDocumentRenderer(true, PdfFontEmbedding.Always); pdfRenderer.Document = document; pdfRenderer.RenderDocument(); pdfRenderer.PdfDocument.Save(filePath);
You may need to use non-default fonts in the document, for example,
DejaVuSansMono.ttf . You can do this by first placing the file of the above font in the same folder where the executable file is:
Uri uri = new Uri(@"file://" + System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + @"\DejaVuSansMono.ttf"); XPrivateFontCollection pfc = XPrivateFontCollection.Global; try { pfc.Add(uri, "./#DejaVu Sans Mono"); } catch (Exception) {} pdfRenderer.DocumentRenderer.PrivateFonts = pfc;
If you need to display the created document in your application,
MigraDoc.Rendering.Windows.DocumentPreview will help us to display the documents
created in MigraDoc . First you need to put it on a form or a WPF window, then use the following code:
Document doc = new Document(); doc = oDocument.Clone(); preview.Ddl = DdlWriter.WriteToString(doc);
The ability to display already created PDF-documents is unfortunately not available.
I recommend to pay attention to the
Clone () function, which is important when assigning any MigraDoc objects (documents, sections, paragraphs, etc.), if you do not use it, then when you change the first object, the one that is assigned will be changed.
To print MigraDoc documents you can use
MigraDocPrintDocument :
MigraDocPrintDocument migraDocPrint = new MigraDocPrintDocument(preview.Renderer)
But such a problem is noticed that this method does not work when printing through a server, printing through a local printer is normal.
Therefore, as an option, you can use Foxit Reader for printing:
pdfDocumentRenderer.PdfDocument.Save(@"temp.pdf"); Process.Start(@"Resources\Foxit Reader.exe", @"/p temp.pdf");
In conclusion, I would like to say that after testing all the libraries from
this post, I settled on this library as the most convenient and well-equipped.