Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open an existing PDF file with Migradoc PDF library

I am trying to use the Migradoc library from PDFSharp (http://www.pdfsharp.net/) to print pdf files. So far I have found that Migradoc does support printing through its MigraDoc.Rendering.Printing.MigraDocPrintDocument class. However, I have not found a way to actually open an existing PDF file with MigraDoc.

I did find a way to open an existing PDF file using PDFSharp, but I cannot successfully convert a PDFSharp.Pdf.PdfDocument into a MigraDoc.DocumentObjectModel.Document object. So far I have not found the MigraDoc and PDFSharp documentation to be very helpful.

Does anyone have any experience using these libraries to work with existing PDF files?

I wrote the following code with help from this sample, but the result when my input PDF is 2 pages is an output PDF with 2 blank pages.

using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;

...

public void PrintPDF(string filePath, string outFilePath)
{

    var document = new Document();

    var docRenderer = new DocumentRenderer(document);
    docRenderer.PrepareDocument();

    var inPdfDoc = PdfReader.Open(filePath, PdfDocumentOpenMode.Modify);

    for (var i = 0; i < inPdfDoc.PageCount; i++)
    {
        document.AddSection();
        docRenderer.PrepareDocument();

        var page = inPdfDoc.Pages[i];

        var gfx = XGraphics.FromPdfPage(page);

        docRenderer.RenderPage(gfx, i+1);
    }

    var renderer = new PdfDocumentRenderer();

    renderer.Document = document;

    renderer.RenderDocument();

    renderer.PdfDocument.Save(outFilePath);

}
like image 406
Brian Avatar asked Oct 31 '22 10:10

Brian


1 Answers

Your code modifies the inPdfDoc in memory without saving the changes. Complicated code without any visual effect.

MigraDoc cannot open PDF files, MigraDoc cannot print PDF files, PDFsharp cannot print PDF files.

http://www.pdfsharp.net/wiki/PDFsharpFAQ.ashx

like image 125
I liked the old Stack Overflow Avatar answered Nov 15 '22 04:11

I liked the old Stack Overflow