使用ASP.NET生成打印质量PDF

时间:2011-06-27 16:47:02

标签: asp.net image printing pdf-generation asp.net-2.0

我最近在网站上创建了一个功能,该功能将生成包含图像和文本的PDF文档。然后将它们打印出来并粘在咖啡杯上。

我使用Winnovative HTML to PDF转换完成了这项工作。

一切都很好,但是图像质量对于打印而言太低了,而且在杯子上看起来很垃圾。

我被告知这是因为图像只有70dpi(屏幕质量),需要300dpi(打印质量)。

有没有办法生成这些PDF,以便图像具有打印质量?

2 个答案:

答案 0 :(得分:3)

我对iTextSharp非常好运,我用它构建了许多优质的pdf。但我从头开始构建pdf,并直接打印。您是否可以在代码中重新创建页面内容并进行打印?这很简单,并且有很多资源/示例可供构建。

如果没有,那里有几个产品,如pdfcrowd。我不能说我自己用过它。但是我听说过人们使用它来获得高质量的html到pdf,但是你必须为这个特权买单。

答案 1 :(得分:0)

当您在PDF中有光栅图像时,其质量会越来越低,您可以放大PDF文档。为了进行良好的比较,您应该在PDF查看器中拥有100%的缩放级别。如果您想要在PDF中使用更高分辨率的图片而不是HTML图片,则可以在Replace Images from HTML with Higher Quality Images in PDF Demo中看到。该演示的相关C#代码是:

protected void convertToPdfButton_Click(object sender, EventArgs e)
{
    // Create a HTML to PDF converter object with default settings
    HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();

    // Set license key received after purchase to use the converter in licensed mode
    // Leave it not set to use the converter in demo mode
    htmlToPdfConverter.LicenseKey = "fvDh8eDx4fHg4P/h8eLg/+Dj/+jo6Og=";

    // Select all images from HTML page
    htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementSelectors = new string[] { "img" };

    // Exclude the original images from rendering becuase they will be replaced by an image from local file system
    htmlToPdfConverter.HiddenHtmlElementsSelectors = new string[] { "img" };

    Document pdfDocument = null;
    try
    {
        // Convert a HTML string with images to replace to a PDF document object
        pdfDocument = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(urlTextBox.Text);

        // Replace the images selected in HTML using special attributes with images from local file system
        foreach (HtmlElementMapping imageElementInfo in htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementsMappingResult)
        {
            PdfPage imagePdfPage = imageElementInfo.PdfRectangles[0].PdfPage;
            RectangleF imageRectangle = imageElementInfo.PdfRectangles[0].Rectangle;

            ImageElement newImageElement = new ImageElement(imageRectangle.X, imageRectangle.Y, imageRectangle.Width, imageRectangle.Height,
                            Server.MapPath("~/DemoAppFiles/Input/Images/box.jpg"));
            newImageElement.EnlargeEnabled = true;
            imagePdfPage.AddElement(newImageElement);
        }

        // Save the PDF document in a memory buffer
        byte[] outPdfBuffer = pdfDocument.Save();

        // Send the PDF as response to browser

        // Set response content type
        Response.AddHeader("Content-Type", "application/pdf");

        // Instruct the browser to open the PDF file as an attachment or inline
        Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Replace_with_Higher_Quality_Images.pdf; size={0}", outPdfBuffer.Length.ToString()));

        // Write the PDF document buffer to HTTP response
        Response.BinaryWrite(outPdfBuffer);

        // End the HTTP response and stop the current page processing
        Response.End();
    }
    finally
    {
        // Close the PDF document
        if (pdfDocument != null)
            pdfDocument.Close();
    }
}