将innerHtml打印到打印机,并在vb.net/C#中完整格式化

时间:2016-08-17 10:43:08

标签: c# html vb.net itext gecko

在vb.net中,我需要将浏览器控件中显示的内容打印到打印机。

我在winform应用程序中使用了Gecko Web浏览器控件,没有直接打印页面内容的方法。

使用-(UIImage*) takeSnapshot { GstSample *videobuffer = NULL; GstCaps *caps; gint width, height; GstMapInfo map; g_object_get(G_OBJECT(video_sink), "last-sample", &videobuffer, NULL); if (videobuffer) { caps = gst_sample_get_caps(videobuffer); if (!caps) { return NULL; } GstStructure *s = gst_caps_get_structure(caps, 0); /* we need to get the final caps on the buffer to get the size */ gboolean res; res = gst_structure_get_int (s, "width", &width); res |= gst_structure_get_int (s, "height", &height); if (!res) { return NULL; } GstBuffer *snapbuffer = gst_sample_get_buffer(videobuffer); if (snapbuffer && gst_buffer_map (snapbuffer, &map, GST_MAP_READ)) { CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, map.data, height * width * 4, NULL); CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; CGImageRef imageRef = CGImageCreate(width, height, 8, 4 * 8, width * 4, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent); UIImage *uiImage = [UIImage imageWithCGImage:imageRef]; CGColorSpaceRelease(colorSpaceRef); CGImageRelease(imageRef); return uiImage; } return NULL; } return NULL; } 直接打印或将InnerHtml转换为pdf,然后打印html文档。

目前我正在使用第三方库`ItextSharpe',但它会出错。

pdf

但在解析时会抛出错误。

public byte[] GetPDF(string pHTML) {
    byte[] bPDF = null;

    MemoryStream ms = new MemoryStream();
    TextReader txtReader = new StringReader(pHTML);

    // 1: create object of a itextsharp document class
    Document doc = new Document(PageSize.A4, 25, 25, 25, 25);

    // 2: we create a itextsharp pdfwriter that listens to the document and directs a XML-stream to a file
    PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms);

    // 3: we create a worker parse the document
    HTMLWorker htmlWorker = new HTMLWorker(doc);

    // 4: we open document and start the worker on the document
    doc.Open();
    htmlWorker.StartDocument();

    // 5: parse the html into the document
    htmlWorker.Parse(txtReader);

    // 6: close the document and the worker
    htmlWorker.EndDocument();
    htmlWorker.Close();
    doc.Close();

    bPDF = ms.ToArray();

    return bPDF;
}
Byte[] bytes;
            bytes = GetPDF(browse.Document.Body.InnerHtml);
            var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");
            System.IO.File.WriteAllBytes(testFile, bytes);

enter image description here enter image description here

我在网上看到了不同的例子,但这完全不同,示例或重复的答案是导出面板或网格,但这是动态HTML,我需要将其转换为PDF或直接打印客户区。

0 个答案:

没有答案