【问题标题】:Adding different sized pages to PDF using iTextSharp使用 iTextSharp 将不同大小的页面添加到 PDF
【发布时间】:2012-02-23 15:00:33
【问题描述】:

我有一个要添加到 PDF 文档的表格和图表。我已经使用 iTextSharpLibrary 将内容添加到 PDF 文件中。

实际上问题在于图表的宽度为 1500 像素,而表格在 A4 页面大小中非常适合。

实际上,我得到的图表图像不能缩放以适应页面,因为它会降低可见度。因此,我需要添加一个宽度比其他页面更宽的新页面,或者至少将页面方向更改为横向,然后添加图像。我该怎么做?

这是我用来添加新页面然后调整页面大小然后添加图像的代码。这是行不通的。有什么修复吗?

var imageBytes = ImageGenerator.GetimageBytes(ImageSourceId);
var myImage = iTextSharp.text.Image.GetInstance(imageBytes);

document.NewPage();

document.SetPageSize(new Rectangle(myImage.Width, myImage.Height));

myImage.ScaleToFit(document.PageSize.Width, document.PageSize.Height);
document.Add(myImage);

【问题讨论】:

  • “这不起作用”是什么意思?你得到一个错误?您是否在 PDF 查看器中检查了页面大小?
  • 你想保持图像大小吗?在您的问题中,您说:“实际上,我得到的图表图像不得缩放以适合页面...”,但在您的代码 sn-p 中,您通过调用 @987654322 与此声明相矛盾@.
  • @OcasoProtal:我没有收到任何错误。但是页面大小没有改变以适应图表
  • @kuujinbo:即使我使用chartImage.ScaleAbsoluteHeight(document.PageSize.Height); chartImage.ScaleAbsoluteWidth(document.PageSize.Width);,导出的图表也没有任何变化
  • 我尝试通过 document.SetPageSize(new Rectangle(...)) 更改页面大小,但这似乎在调用 PdfDocument.CreateInstance 之前有效。

标签: c# pdf itextsharp


【解决方案1】:

我解决了这个问题。我必须在调用 Pdfdocument 的 GetInstance 之前设置页面大小。然后,我可以为每个页面提供不同的页面大小

【讨论】:

    【解决方案2】:

    我不确定before calling the GetInstance of the Pdfdocument 是什么意思,但是在调用 newPage 之前设置 pageSize 是可行的。 下面是创建由两张图片组成的新 pdf 的 c# 代码,无论它们的大小差异有多大。这里重要的几行是new DocumentSetPageSize

    static public void MakePdfFrom2Pictures (String pic1InPath, String pic2InPath, String pdfOutPath)
    {
        using (FileStream pic1In = new FileStream (pic1InPath, FileMode.Open))
        using (FileStream pic2In = new FileStream (pic2InPath, FileMode.Open))
        using (FileStream pdfOut = new FileStream (pdfOutPath, FileMode.Create))
        {
            //Load first picture
            Image image1 = Image.GetInstance (pic1In);
            //I set the position in the image, not during the AddImage call
            image1.SetAbsolutePosition (0, 0);
            //Load second picture
            Image image2 = Image.GetInstance (pic2In);
            // ...
            image2.SetAbsolutePosition (0, 0);
            //Create a document whose first page has image1's size.
            //Image IS a Rectangle, no need for new Rectangle (Image.Width, Image.Height).
            Document document = new Document (image1);
            //Assign writer
            PdfWriter writer = PdfWriter.GetInstance (document, pdfOut);
            //Allow writing
            document.Open ();
            //Get writing head
            PdfContentByte pdfcb = writer.DirectContent;
            //Put the first image on the first page
            pdfcb.AddImage (image1);
            //The new page will have image2's size
            document.SetPageSize (image2);
            //Add the new second page, and start writing in it
            document.NewPage ();
            //Put the second image on the second page
            pdfcb.AddImage (image2);
            //Finish the writing
            document.Close ();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-05-06
      • 1970-01-01
      • 2015-04-02
      • 1970-01-01
      • 2015-05-14
      • 2018-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多