简体   繁体   English

使用 iTextSharp 将不同大小的页面添加到 PDF

[英]Adding different sized pages to PDF using iTextSharp

I have a table and a chart to be added to a PDF Document.我有一个表格和一个图表要添加到 PDF 文档中。 I have used the iTextSharpLibrary to add the contents to the PDF file.我已经使用 iTextSharpLibrary 将内容添加到 PDF 文件中。

Actually the problem is that the chart has a width of 1500px and the table fits in comfortably in an A4 page size.实际上,问题在于图表的宽度为 1500 像素,并且表格适合 A4 页面大小。

Actually the chart image that I get must not be scaled to fit in the page as it reduces the viewability.实际上,我获得的图表图像不能缩放以适应页面,因为它会降低可见度。 Hence, I need to add a new page that has a wider width than the other ones or at least change the page orientation to landscape and then add the image.因此,我需要添加一个宽度比其他页面更宽的新页面,或者至少将页面方向更改为横向,然后添加图像。 How do I do this?我该怎么做呢?

This is the code that I used to add a new page and then resize the page and then add the image.这是我用来添加新页面,然后调整页面大小,然后添加图像的代码。 This is not working.这是行不通的。 Any fixes?任何修复?

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

I fixed the Issue.我修复了问题。 I have to set the page size before calling the GetInstance of the Pdfdocument.我必须在调用 Pdfdocument 的 GetInstance 之前设置页面大小。 Then, i can give different pagesizes for each page然后,我可以为每个页面提供不同的页面大小

I'm not sure what you mean by before calling the GetInstance of the Pdfdocument , but setting the pageSize right before calling newPage works. before calling the GetInstance of the Pdfdocument我不确定您的意思,但是在调用 newPage 之前设置 pageSize 有效。 Here follows the c# code to create a new pdf that's made of two pictures, no matter how wild their sizes difference is.这里遵循 c# 代码创建一个由两张图片组成的新 pdf,无论它们的大小差异有多大。 The important lines here are the new Document and SetPageSize ones.这里重要的SetPageSize行是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 ();
    }
}

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

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