繁体   English   中英

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

[英]MigraDoc and PDFsharp showing different documents when saving as PDF and image

保存时,图像的设计正确,但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);
    }

您调用docRenderer.RenderPage(gfx, 1); 仅用于图像。 这将呈现标题。

您不调用docRenderer.RenderPage(gfx, 1); 用于PDF。 因此,那里没有日期,只有您之前绘制的“ TEST TEST”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM