MigraDoc and PDFsharp showing different documents when saving as PDF and image

1k views Asked by At

When saving, the image has the right design but the PDF has the wrong text on it. Could you explain why the documents are different?

I'm also open to other solutions for saving a PDF of the whole document and the ability to print a selected page of a multi-page document.

thanks :)

EDIT: the image is showing the date ("24th May 2016") which is correct and what I want the PDF to show, but instead the PDF is showing "TEST TEST"

1

public static void pdf() {
        DateTime now = DateTime.Now;
        string filename = "MixMigraDocAndPdfSharp.pdf";
        filename = Guid.NewGuid().ToString("D").ToUpper() + ".pdf";
        PdfDocument document = new PdfDocument();

        SamplePage1(document);

        document.Save(filename);

        Process.Start(filename);

    }

2

    static void SamplePage1(PdfDocument document) {
        PdfPage page = document.AddPage();
        XGraphics gfx = XGraphics.FromPdfPage(page);
        gfx.MUH = PdfFontEncoding.Unicode;
        gfx.MFEH = PdfFontEmbedding.Default;

        XFont font = new XFont("Verdana", 13, XFontStyle.Bold);
        gfx.DrawString("TEST TEST", font, XBrushes.Black,
                        new XRect(100, 100, page.Width - 200, 300), XStringFormats.Center);

        Document doc = new Document();
        Section sec = doc.AddSection();
        Paragraph para = sec.AddParagraph();

        header("24th May 2016");

        DocumentRenderer docRenderer = new DocumentRenderer(doc);
        docRenderer.PrepareDocument();

        docRenderer.RenderObject(gfx, XUnit.FromCentimeter(5), XUnit.FromCentimeter(10), "12cm", para);
        PageInfo info = docRenderer.FormattedDocument.GetPageInfo(1);

        int dpi = 150;
        int dx, dy;
        if (info.Orientation == PdfSharp.PageOrientation.Portrait) {
            dx = (int)(info.Width.Inch * dpi);
            dy = (int)(info.Height.Inch * dpi);
        } else {
            dx = (int)(info.Height.Inch * dpi);
            dy = (int)(info.Width.Inch * dpi);
        }

        Image image = new Bitmap(dx, dy, PixelFormat.Format32bppRgb);
        Graphics graphics = Graphics.FromImage(image);
        graphics.Clear(System.Drawing.Color.White);
        float scale = dpi / 72f;
        graphics.ScaleTransform(scale, scale);
        gfx = XGraphics.FromGraphics(graphics, new XSize(info.Width.Point, info.Height.Point));
        docRenderer.RenderPage(gfx, 1);
        gfx.Dispose();
        image.Save("test.png", ImageFormat.Png);
        doc.BindToRenderer(docRenderer);
        docRenderer.RenderObject(gfx, XUnit.FromCentimeter(5), XUnit.FromCentimeter(10), "12cm", para);
        Process.Start("mspaint", "test.png");         
    }

3

    public static void header(String date) {
        Paragraph paragraph = new Paragraph();

        var dateIssued = firstPage.AddTextFrame();
        dateIssued.Height = "1.0cm";
        dateIssued.Width = "6.0cm";
        dateIssued.Left = "2.1cm";
        dateIssued.RelativeHorizontal = RelativeHorizontal.Margin;
        dateIssued.Top = "3.55cm";
        dateIssued.RelativeVertical = RelativeVertical.Page;
        paragraph = dateIssued.AddParagraph(date);
    }
1

There are 1 answers

3
I liked the old Stack Overflow On BEST ANSWER

You call docRenderer.RenderPage(gfx, 1); for the image only. This renders the header.

You do not call docRenderer.RenderPage(gfx, 1); for the PDF. So no date there, just the "TEST TEST" you draw earlier.