2017-08-29 72 views
0

保存時,圖像具有正確的設計,但PDF上的文本錯誤。你能解釋爲什麼這些文件是不同的?當保存爲PDF和圖像時,MigraDoc和PDFsharp顯示不同的文檔

我也接受其他解決方案來保存整個文檔的PDF以及打印多頁文檔的選定頁面的能力。

謝謝:)

編輯:圖像顯示日期(「2016年5月24日」),這是正確的,我想要的PDF顯示,而是在PDF中顯示「TEST TEST」

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); 

    } 
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");   
    } 
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); 
    } 
+0

不清楚你在問什麼。顯示你所得到的圖像,並解釋與你的期望有什麼不同可能會有所幫助。一個MCVE也可以幫助:https://stackoverflow.com/help/mcve –

+0

我已經添加了一些關於每種格式顯示的說明。我試圖將整個文檔保存爲PDF格式,並且單獨打印每個PDF頁面,目前我正在通過圖像打印每個頁面(可怕的解決方案,但我找不到更好的方法) –

+0

是否存在混合MigraDoc的特定原因和PDFsharp?儘量保持簡單,只需按照預期使用MigraDoc。代碼應該在PDF中繪製「測試測試」。您不要調用繪製PDF案例頭的代碼。 –

回答

0

您只能撥打docRenderer.RenderPage(gfx, 1);的圖像。這呈現標題。

您不要致電docRenderer.RenderPage(gfx, 1);的PDF。所以沒有日期,只是你先前繪製的「測試測試」。

+0

我已經取得了一些進展,而不是將其轉換爲圖像,我可以使用MigraDoc打印文檔(我認爲?)我唯一的問題是獲取printDocument.SelectedPage MigraDoc網站上的源代碼使用this.pagePreview。頁。我還能如何獲得所選頁面? –

+0

不要設置'SelectedPage'來打印整個文檔。據我所知,反正只有一頁。 –

+0

感謝您的幫助! –

相關問題