winforms 如何在VS代码窗口窗体中添加pdfmerger程序集引用?

z3yyvxxp  于 2023-04-21  发布在  其他
关注(0)|答案(1)|浏览(94)

我正在尝试将PDF合并在一起。我写了这行代码来创建PDF合并类的示例:

PdfMerger merger = new PdfMerger();

这行代码是在windows窗体上的按钮单击操作下调用的。我还添加了iTextSharp包。
但我犯了这个错误:
[错误消息](https://i.stack.imgur.com/xM2kC.png
我试着添加一个iTextSharp的参考,但它似乎仍然不起作用。

des4xlb0

des4xlb01#

我使用iTextSharp和此功能合并PDF文件

using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;


        public static async Task<bool> MergePdfFiles(string[] pdfFiles, string outputPath, string authorName = "", string creatorName = "", string subject = "", string title = "", string keywords = "")
    {
        var result = false;
        var f = 0;            
        var bookmarkFont = iTextSharp.text.FontFactory.GetFont(iTextSharp.text.FontFactory.HELVETICA, 12, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLUE);
        try
        {
            var pdfCount = pdfFiles.Length;     
            if (pdfCount > 0)
            {
                var fileName = pdfFiles[f];  
                var reader = new PdfReader(fileName);
                var pageCount = reader.NumberOfPages;   
                pageCount = GetNumberOfPdfPages(fileName);

                var pdfDoc = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(1), 18, 18, 18, 18);    // the output pdf document
                var writer = PdfWriter.GetInstance(pdfDoc, new FileStream(outputPath, FileMode.Create));
                pdfDoc.AddAuthor(authorName);
                pdfDoc.AddCreationDate();
                pdfDoc.AddCreator(creatorName);
                pdfDoc.AddProducer();
                pdfDoc.AddSubject(subject);
                pdfDoc.AddTitle(title);
                pdfDoc.AddKeywords(keywords);
                pdfDoc.Open();
                var cb = writer.DirectContent;

                while (f < pdfCount)
                {
                    int i = 0;
                    while (i < pageCount)
                    {
                        i += 1;
                        pdfDoc.SetPageSize(reader.GetPageSizeWithRotation(i));
                        pdfDoc.NewPage();

                        var page = writer.GetImportedPage(reader, i);
                        var rotation = reader.GetPageRotation(i);
                        if (rotation == 90)
                        {
                            cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                        }
                        else if (rotation == 270)
                        {
                            cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, reader.GetPageSizeWithRotation(i).Width + 60, -30);
                        }
                        else
                        {
                            cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0);
                        }
                    }
                    f += 1;
                    if (f < pdfCount)
                    {
                        fileName = pdfFiles[f];
                        reader = new PdfReader(fileName);
                        pageCount = reader.NumberOfPages;
                        pageCount = GetNumberOfPdfPages(fileName);
                    }
                }
                pdfDoc.Close();
                result = true;
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }

        return result;
    }

相关问题