FAQ - Frequently asked questions
Version 6.2.0
Questions and answers about PDFsharp
In this article
- warning CS0618: 'XUnit.implicit operator XUnit(int)' is obsolete
- warning CS0618: 'XUnit.implicit operator double(XUnit)' is obsolete
- Can I reduce the size of my generated PDFs?
- Can PDFsharp show PDF files? Print PDF files? Create images from PDF files?
- Font style Bold, Italic and Underline together?
- Can I use PostScript fonts with PDFsharp?
- Which PDF versions are supported by PDFsharp?
- Does PDFsharp support PDF/A?
- Does PDFsharp support AcroForms?
- Can I use PDFsharp to convert HTML or RTF to PDF?
- Can I use PDFsharp to convert PDF to Word, RTF, HTML?
- Can I use PDF files created with SQL Server 2008 Reporting Services?
- How to use an image from a byte[]?
- Can I use PDFsharp to extract text from PDF?
- Can I use PDFsharp to edit PDF contents?
- Can PDFsharp simulate Bold or Italics?
- How many DPI are there in a PDF file? How can I set the DPI?
About
See the PDFsharp Overview for general information about PDFsharp.
See also:
General FAQ
MigraDoc FAQ
warning CS0618: 'XUnit.implicit operator XUnit(int)' is obsolete
warning CS0618: 'XUnit.implicit operator double(XUnit)' is obsolete
Implicit operators allow the compiler to perform some conversions automatically.
Some implicit operators have been marked as "obsolete" with version 6.1 as the results are sometimes counter-intuitive.
It can be a bit difficult to find where these implicit operators are used as the compiler invokes them automatically as needed.
What does this warnings mean for you? With version 6.2, it is just a warning and your program will behave as before.
With future versions however, the implicit conversions may be removed and your code will no longer compile.
Let’s look at this code:
var u = XUnit.FromInch(1);
double d = u + 1;
Which value would you expect to see in variable d?
The answer is not 2, the answer is 73. The value of u is one inch. One inch is 72 points. The implicit conversion for XUnit returns the point value.
You can avoid the warning by adding .Value
:
var u = XUnit.FromInch(1);
double d = u.Value + 1;
In this case, the value of d will be 2.
You can avoid the warning by adding .Point
:
var u = XUnit.FromInch(1);
double d = u.Point + 1;
In this case, the value of d will be 73, just what it was with the implicit conversion. But now it is clear that 1 inch will be converted to points.
Can I reduce the size of my generated PDFs?
PDFsharp activates compression of PDF files by default. The default settings are a trade-off between compressed file size and required execution time. You can change the settings to get PDF files in less CPU time, but files will then be larger. You can change the settings to get smaller PDF files, but execution will take longer.
Do not expect miracles: With maximized compression settings, you can expect a size reduction in the range 1% to 5%.
Here are some settings you can try:
document.Options.EnableCcittCompressionForBilevelImages = false;
document.Options.FlateEncodeMode =
PdfFlateEncodeMode.BestCompression;
document.Options.UseFlateDecoderForJpegImages =
PdfUseFlateDecoderForJpegImages.Automatic;
Make sure to change those settings directly after creating the PdfDocument instance and before adding any contents.
Can PDFsharp show PDF files? Print PDF files? Create images from PDF files?
PDFsharp comes with a preview control designed to visualize drawing operations of the XGraphics object, but it cannot render PDF files.
Further the DrawImage function can be used to draw so called form XObjects in PDF pages. If you try to render such an object in the preview, only the bounding box is drawn to show that it cannot be rendered.
The PDFsharp Samples show how to invoke Adobe Reader or Acrobat to view or print PDF files and how to invoke GhostScript to create images from PDF pages.
Font style Bold, Italic and Underline together?
XFontStyleEx is an enum and you can use the binary or operator to combine flags.
var boldUnderlineFont = new XFont("Arial", 10,
XFontStyleEx.Bold | XFontStyleEx.Underline);
Can I use PostScript fonts with PDFsharp?
PDFsharp cannot work with PostScript fonts. Only TrueType fonts and OpenType fonts with TrueType outlines can be used with PDFsharp. Read more...
Which PDF versions are supported by PDFsharp?
With PDFsharp you can create files with PDF versions from 1.4 (Adobe Acrobat Reader 5.0) through 1.7 (Adobe Reader 8.0) and version 2.0. PDFsharp fully supports PDF 1.4 (Adobe Reader 5.0) including the transparency features introduced with this version. PDFsharp does not yet write object streams. When processing files containing object streams with PDFsharp, this can lead to increased file size after saving the file.
Does PDFsharp support PDF/A?
Not yet with PDFsharp 6.1.
Does PDFsharp support AcroForms?
There is limited support for AcroForms included with PDFsharp 6.1.
Can I use PDFsharp to convert HTML or RTF to PDF?
No, not "out of the box", and we do not plan to write such a converter in the near future.
Yes, PDFsharp can do it with some extra code. But we do not supply that extra code. On NuGet and other sources you can find a third party library "HTML Renderer for PDF using PdfSharp" that converts HTML to PDF. And there may be other libraries for the same or similar purposes, too. Maybe they work for you, maybe they get you started.
Can I use PDFsharp to convert PDF to Word, RTF, HTML?
No, and we do not plan to write such a converter in the near future.
Can I use PDF files created with SQL Server 2008 Reporting Services?
There is an issue with the PDFs created by SQL Server 2008 Reporting Services. We are working on it.
As a workaround, create reports with SQL Server 2005 Reporting Services. Workaround for SQL Server 2008 Reporting Services: For the DeviceSettings parameter for the Render method on the ReportExecutionService object, pass this value:
theDeviceSettings = "
How to use an image from a byte[]?
Just create a MemoryStream from the byte[] and use that with XImage.FromStream.
// byte[] imgdata contains the image.
using (MemoryStream ms =
new MemoryStream(imgdata, 0, imgdata.Length, false, true))
{
using (XImage img = XImage.FromStream(ms))
{
gfx.DrawImage(img, ip.dx, ip.dy, ip.dw, ip.dh);
}
}
Can I use PDFsharp to extract text from PDF?
This can be done at a low level. You can get at the characters in the order they are drawn - and most applications draw them from top-left to bottom-right. There are no high-level functions that return words, paragraphs, or whole pages.
Have a look at the CLexer class.
Can I use PDFsharp to edit PDF contents?
Just like PDFsharp does not have APIs for text extractions, there neither are API for content manipulation. Similar to text extraction, you can build up on the PDFsharp methods to manipulate the contents of PDF pages. But so far there are no helpers that deal with coordinates and other details.
Have a look at the CLexer class.
Can PDFsharp simulate Bold or Italics?
Yes, when using a FontResolver.
How many DPI are there in a PDF file? How can I set the DPI?
PDF is a vector format, so there are no DPI. Raster images used in a PDF file do have DPI, but DPI is determined by the usage. Consider an image with 300 DPI. This image can be embedded once in the PDF file, but can be drawn several times. There could be a thumbnail on page 1, a full size reproduction on page 2, and a double size reproduction on page 3. Thus the image is drawn with 600 DPI on page 1, 300 DPI on page 2, and 150 DPI on page 3. But when you watch the PDF file in Adobe Reader with a Zoom factor of 1000%, the DPI value will be much lower than that. PDF is vector. There is no DPI. PDFsharp uses Points as the unit for coordinates. There are 72 Points per Inch. For ease of use, units can be converted from Inch, Centimeter, Millimeter and other units.