如何将PDF文件从二进制文件附加到已经';进行中';PDF,使用iTextSharp

本文关键字:PDF iTextSharp 使用 进行中 文件 二进制文件 | 更新日期: 2023-09-27 18:28:21

在代码中,我正在使用iTextSharp创建一个PDF文档。我已经将内容添加到文档中,并关闭了文档,在对web浏览器的响应中成功检索到该文档。

我想做的是将另一个PDF文档附加到我正在创建的文档中,但它必须来自二进制或Byte[]类型的对象。

我意识到有可用的方法document.Add(stuff),但我正在尝试将二进制文件转换为对象,然后将其添加到正在进行的文档中。我看到过类似于我的场景的问题和帖子,但它们大多是关于图像的。

这是我的

while (sqlExpDocDataReader.Read())
{
    // Read data and fill temp. objects
    string docName = sqlExpDocDataReader["docName"].ToString();
    string docType = sqlExpDocDataReader["docType"].ToString();
    Byte[] docData = (Byte[])sqlExpDocDataReader["docData"];
    // Get current page size
    var pageWidth = document.PageSize.Width;
    var pageHeight = document.PageSize.Height;
    // Is this an image or PDF?
    if (docType.Contains("pdf"))
    {
        // Could I use a memeory stream some how?
        MemoryStream ms = new MemoryStream(docData.ToArray());
    }
    else
    {
        // Here I see how to do it with images.
        Image doc = Image.GetInstance(docData);
        doc.ScaleToFit(pageWidth, pageHeight); // width, height
        document.Add(doc);
    }
}

有什么想法吗?

如何将PDF文件从二进制文件附加到已经';进行中';PDF,使用iTextSharp

通过更多的挖掘,以下是我如何解决我的问题。。。

基本上,我从二进制数据中创建了一个MemoryStream对象,然后创建了PdfReader来读取该对象,通常我们会在其中读取一个文件。然后,我循环浏览阅读器对象(或者文件,如果你愿意的话)的每一页,并在找到它们时将它们附加在一起。

if (docType.Contains("pdf"))
{
    MemoryStream ms = new MemoryStream(docData.ToArray());
    PdfReader pdfReader = new PdfReader(ms);
    for (int i = 1; i <= pdfReader.NumberOfPages; i++)
    {
        PdfImportedPage page = writer.GetImportedPage(pdfReader, i);
        document.Add(iTextSharp.text.Image.GetInstance(page));
    }
}
public static byte[] UnificarImagenesPDF(IEnumerable<DocumentoDTO> documentos)// "documents" is a list of objects that are located in the database, the images and pdf are stored in a binary attribute of "documents" 
{
 using (MemoryStream workStream = new MemoryStream())
 {
  iTextSharp.text.Document doc = new iTextSharp.text.Document();//to create a itextSharp Document
  PdfWriter writer = PdfWriter.GetInstance(doc, workStream);
  doc.Open();
  foreach (DocumentoDTO d in documentos)// "documentos" has an attribute where the document extension type is saved (eg pdf, jpg, png, etc) 
  {
   try
   {
    if (d.sExtension == ".pdf")
  {
   MemoryStream ms = new MemoryStream(d.bBinarios.ToArray());
   PdfReader pdfReader = new PdfReader(ms); // 
    for (int i = 1; i <= pdfReader.NumberOfPages; i++)
   {
   PdfImportedPage page = writer.GetImportedPage(pdfReader, i);
   doc.Add(resizeImagen(iTextSharp.text.Image.GetInstance(page)));//Each sheet of the PDF document is added to the document created in itextsharp, and the resizeImage function is used so that the images are centered in the ITEXTSHARP document
   doc.NewPage();// add a new page on ITEXTSHARP document
   }
  }
if (d.sExtension != ".pdf")
 {
  doc.Add(resizeImagen(Image.GetInstance((byte[])d.bBinarios)));
  doc.NewPage();
 }
}
 catch
 {  }
}
   doc.Close();
   writer.Close();
   return workStream.ToArray();
}
}
private static iTextSharp.text.Image resizeImagen(iTextSharp.text.Image image)
{
   if (image.Height > image.Width)
   {
   //Maximum height is 800 pixels.
   float percentage = 0.0f;
   percentage = 700 / image.Height;
   image.ScalePercent(percentage * 100);
   }
   else
   {
  //Maximum width is 600 pixels.
  float percentage = 0.0f;
  percentage = 540 / image.Width;
  image.ScalePercent(percentage * 100);
   }
  return image;
}