最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

itext - .Net project using iTextSharp v4 adding Word doc to a PDF - Stack Overflow

programmeradmin5浏览0评论

I'm needing to append a Word doc to a PDF. I'm stuck using iTextsharp v4.1.6.0, and cannot at the moment upgrade this to a newer version. This is also for a c#.Net using framework 4.6.2.

I'm hoping there is something in this iTextsharp version that would allow me to add a Word doc to a PDF. Any Help would be greatly appreciated.

Here's some examples of how this is being used for adding a plain text file, an image, and another PDF to the one being created (as well as the creation of the document).

//iTextSharp Document object
var document = new iTextSharp.text.Document(PageSize.LETTER, 50, 50, 50, 50);

//Open document for writing
document.Open();

// Additional content for PDF stuff

//Adding additional Files to PDF
foreach (additionalFiles a in additionalFiles)
{
    document.NewPage();
    // plain text
    if (a.MimeType == "text/plain")
    {
        StreamReader str = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(a.FilePath));
        document.Add(new Paragraph(str.ReadToEnd()));
        str.Close();
    }
    // images
    else if (a.MimeType == "image/png" || a.MimeType == "image/jpeg" || a.MimeType == "image/gif")
    {
        var aImage = Image.GetInstance(System.Web.HttpContext.Current.Server.MapPath(a.FilePath));
        document.Add(aImage);
    }
    // PDF's
    else if (a.MimeType == "application/pdf")
    {
        PdfReader reader = new PdfReader(System.Web.HttpContext.Current.Server.MapPath(a.FilePath), null);
        var pageCount = reader.NumberOfPages;
        for (int i = 1; i <= pageCount; i++)
        {

            PdfImportedPage page = writer.GetImportedPage(reader, i);
            PdfContentByte cb = writer.DirectContent;
            cb.AddTemplate(page, 0, 0);
            if (i != pageCount)
            {
                document.NewPage();
            }
        }
        reader.Close();
    }
    //...Additional Code
}
发布评论

评论列表(0)

  1. 暂无评论