In the previous project, we used wkhtmltopdf to render web pages to generate PDF files. This scheme has not been very stable, and the styles of different scenes are often different, so it needs to be adjusted. Today, we have studied the scheme of generating PDF directly by C #, which is relatively simple. The overall scheme is as follows:
Generate XPS file through WPF library
Convert XPS file into PDF file through pdfsharp
first, take a look at the code of generating XPS file. The code is as follows:
var fixedDoc = new FixedDocument();
var pageContent = new PageContent();
var fixedPage = new FixedPage();
fixedPage.Children.Add(canvas);
((IAddChild)pageContent).AddChild(fixedPage);
fixedDoc.Pages.Add(pageContent);
using var xpsd = new XpsDocument(@"r:\3.xps", FileAccess.ReadWrite);
var xw = XpsDocument.CreateXpsDocumentWriter(xpsd);
xw.Write(fixedDoc);
xpsd.Close();
Because visual in WPF can be converted to XPS file. Thanks to the powerful display ability of WPF, even rendering complex XPS files is very easy.
After having the XPS file, the next step is to convert it to PDF. Here, I use the free pdfsharp package. Because I use. Net 5, I introduce PDF PdfSharp.Xps.dotNet . core, the code is relatively simple, a line of code can be done.
PdfSharp.Xps.XpsConverter.Convert(@"r:\3.xps", @"r:\3.pdf", 0);
The scheme of generating PDF is simple and easy to use with the help of class library of WPF platform, and can realize visualization. The disadvantage is that you can’t run on Linux. If you want to realize PDF generation on Linux platform, you can also use pdfsharp directly. For details, please refer to this article: PDF generation and printing in. Net
The above is the detailed content of the method for C to generate PDF. For more information about C to generate PDF, please refer to
Read More:
- How to solve the problem that the output results of C + + program written in VS2010 flash by
- Python calls C to generate so library and reports an error: undefined symbol
- How to generate and view SSH keys in Ubuntu 16.04
- How to generate UML Diagrams from Java code in Eclipse
- How to generate main () method in java graphical interface
- C # generate data in DataGridView into XML file
- Vs compile VTK, only generate install error, stop, ask God how to solve?
- Converting PDF file to JPG image in Ubuntu
- How to generate lib library with keil MDK and use lib Library
- Error converting jupyter notebook to PDF
- PHP uses timthumb to generate thumbnails
- How to print how to output Int64_ t,uint64_ The value of T in C
- The solution of PDF file unable to display thumbnail in win7
- Error in compiling rmarkdown to PDF file` xxx.sty ‘ not found.
- Problem solving: PDF cannot be opened, and acrobat failed to connect to a DDE server appears
- rmd to pdf error: pandoc document conversion failed with error 43
- How to get all the keys of map by golang
- How to use C + + function pointer array
- C#: How to Use Httpclient to upload files with other parameters
- How to solve the import failure by prompting beautifulsoup under pychar