保存为PDF和图像时,MigraDoc和PDFsharp显示不同的文档

时间:2017-08-29 07:26:08

标签: c# pdfsharp migradoc

保存时,图像设计正确,但PDF上的文字错误。你能解释为什么文件不同吗?

我还可以使用其他解决方案来保存整个文档的PDF以及打印多页文档的选定页面的功能。

谢谢:)

编辑:图像显示日期(" 2016年5月24日")这是正确的以及我希望PDF显示的内容,而是PDF显示" 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 个答案:

答案 0 :(得分:0)

仅为图片调用docRenderer.RenderPage(gfx, 1);。这会呈现标题。

您不会为PDF调用docRenderer.RenderPage(gfx, 1);。所以没有日期,只有" TEST TEST"你早点画。