Is it possible to convert PDF to a vector image format that can be printed with .NET?

We have a .NET application that prints both real printers and PDF, currently uses PDFsharp, although this part can be changed if there is a better option. Most of the output generates text or images, but there may be one or more pages that are added to the end. This page is provided by the end user in PDF format.

When printing on paper, our users use pre-printed paper, but in the case of exported PDF, we combine these pages to the end, since they are already in PDF format.

We want to be able to embed these PDF files directly into the print stream so that they do not need pre-printed paper. However, there are no good options for rendering a PDF to a GDI page (System.Drawing.Graphics).

Is there a vector format that PDF could convert to some external program that could be displayed on a GDI + page without degradation by first converting to a raster image?

+6
source share
4 answers

In an article entitled " " How to Convert PDF to EMF in .NET "," I showed how to do this using our PDFOne.NET product. EMF is a vector graphic, and you can display them on a printer canvas.

A simpler alternative for you is the PDF overlay, described in another article entitled " PDF Overlay - Stitch PDF Pages Together in .NET ." PDFOne allows xy offsets in overlays that let you embroider pages at the edges. In the article here, I overlaid the pages one on top of another, setting the offsets to zero. You set it to the width and height of the page.

DISCLAIMER: I work for the Gnostis.

+2
source

Ghostscript can output PostScript (which is a vector file), which can be directly sent to some types of printers. For example, if you use a printer with LPR support, the PS file can be directly configured for this printer using something like this project: http://www.codeproject.com/KB/printing/lpr.aspx

There are also some commercial options that PDF can print (although I'm not sure if the internal mechanism is based on vector or bitmap images), for example http://www.tallcomponents.com/pdfcontrols2-features.aspx or http: // www. tallcomponents.com/pdfrasterizer3.aspx

0
source

Finally, I realized that there is an option that concerns my general requirement to embed a vector format in a print job, but it does not work with GDI-based printing.

The XPS file format created by the Microsoft XPS Writer print driver can be printed from WPF using the ReachFramework.dll file included with .NET. Using WPF for printing instead of GDI, you can embed an XPS document page in a larger print document. A.

The disadvantage is that working with WPF works quite a bit, so all the support code that directly uses the material in the Sytem.Drawing namespace needs to be rewritten.

Here are the basic ways to implement an XPS document:

Open the document:

XpsDocument xpsDoc = new XpsDocument(filename, System.IO.FileAccess.Read); var document = xpsDoc.GetFixedDocumentSequence().DocumentPaginator; // pass the document into a custom DocumentPaginator that will decide // what order to print the pages: var mypaginator = new myDocumentPaginator(new DocumentPaginator[] { document }); // pass the paginator into PrintDialog.PrintDocument() to do the actual printing: new PrintDialog().PrintDocument(mypaginator, "printjobname"); 

Then create a descendant of DocumentPaginator that will do your actual print. Override abstract methods, in particular GetPage should return DocumentPages in the correct order. Here's my proof of concept code demonstrating how to add custom content to your Xps docs list:

 public override DocumentPage GetPage(int pageNumber) { for (int i = 0; i < children.Count; i++) { if (pageNumber >= pageCounts[i]) pageNumber -= pageCounts[i]; else return FixFixedPage(children[i].GetPage(pageNumber)); } if (pageNumber < PageCount) { DrawingVisual dv = new DrawingVisual(); var dc = dv.Drawing.Append(); dc = dv.RenderOpen(); DoRender(pageNumber, dc); // some method to render stuff to the DrawingContext dc.Close(); return new DocumentPage(dv); } return null; } 

When you try to print in another XPS document, it gives the exception "FixedPage cannot contain another FixedPage", and the H. Alipurian message shows how to fix it: http://social.msdn.microsoft.com/Forums/da/wpf/thread/ 841e804b-9130-4476-8709-0d2854c11582

 private DocumentPage FixFixedPage(DocumentPage page) { if (!(page.Visual is FixedPage)) return page; // Create a new ContainerVisual as a new parent for page children var cv = new ContainerVisual(); foreach (var child in ((FixedPage)page.Visual).Children) { // Make a shallow clone of the child using reflection var childClone = (UIElement)child.GetType().GetMethod( "MemberwiseClone", BindingFlags.Instance | BindingFlags.NonPublic ).Invoke(child, null); // Setting the parent of the cloned child to the created ContainerVisual // by using Reflection. // WARNING: If we use Add and Remove methods on the FixedPage.Children, // for some reason it will throw an exception concerning event handlers // after the printing job has finished. var parentField = childClone.GetType().GetField( "_parent", BindingFlags.Instance | BindingFlags.NonPublic); if (parentField != null) { parentField.SetValue(childClone, null); cv.Children.Add(childClone); } } return new DocumentPage(cv, page.Size, page.BleedBox, page.ContentBox); } 

Sorry this is not entirely compiling code, I just wanted to provide an overview of the code fragments needed to get it working, to give other people the opportunity to start all the disparate pieces that need to come together to make it work. Trying to create a more generalized solution will be much more complicated than the scope of this answer.

0
source

Although it is not open source, not .NET native (I suppose I believe it is on Delphi, but it offers a precompiled .NET library), Quick PDF can display the PDF in an EMF file that you can load into your graphic object.

-1
source

Source: https://habr.com/ru/post/895362/


All Articles