<legend id='NdD9m'><style id='NdD9m'><dir id='NdD9m'><q id='NdD9m'></q></dir></style></legend>

  • <small id='NdD9m'></small><noframes id='NdD9m'>

        • <bdo id='NdD9m'></bdo><ul id='NdD9m'></ul>

      1. <i id='NdD9m'><tr id='NdD9m'><dt id='NdD9m'><q id='NdD9m'><span id='NdD9m'><b id='NdD9m'><form id='NdD9m'><ins id='NdD9m'></ins><ul id='NdD9m'></ul><sub id='NdD9m'></sub></form><legend id='NdD9m'></legend><bdo id='NdD9m'><pre id='NdD9m'><center id='NdD9m'></center></pre></bdo></b><th id='NdD9m'></th></span></q></dt></tr></i><div id='NdD9m'><tfoot id='NdD9m'></tfoot><dl id='NdD9m'><fieldset id='NdD9m'></fieldset></dl></div>
        <tfoot id='NdD9m'></tfoot>

        使用 itextsharp 将页面插入现有 PDF

        时间:2023-06-05
        <tfoot id='rERi7'></tfoot><legend id='rERi7'><style id='rERi7'><dir id='rERi7'><q id='rERi7'></q></dir></style></legend>

          • <bdo id='rERi7'></bdo><ul id='rERi7'></ul>

              • <small id='rERi7'></small><noframes id='rERi7'>

                  <i id='rERi7'><tr id='rERi7'><dt id='rERi7'><q id='rERi7'><span id='rERi7'><b id='rERi7'><form id='rERi7'><ins id='rERi7'></ins><ul id='rERi7'></ul><sub id='rERi7'></sub></form><legend id='rERi7'></legend><bdo id='rERi7'><pre id='rERi7'><center id='rERi7'></center></pre></bdo></b><th id='rERi7'></th></span></q></dt></tr></i><div id='rERi7'><tfoot id='rERi7'></tfoot><dl id='rERi7'><fieldset id='rERi7'></fieldset></dl></div>
                    <tbody id='rERi7'></tbody>

                  本文介绍了使用 itextsharp 将页面插入现有 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我们正在使用 itextsharp 从多个 PDF 文件创建一个 PDF.如何将新页面插入到文件中已有多个页面的 PDF 文件中?当我使用添加页面时,它会覆盖现有页面,并且只保存选择的 1 页.

                  We are using itextsharp to create a single PDF from multiple PDF files. How do I insert a new page into a PDF file that has multiple pages already in the file? When I use add page it is overwriting the existing pages and only saves the 1 page that was selected.

                  这是我用来将页面添加到现有 PDF 的代码:

                  Here is the code that I am using to add the page to the existing PDF:

                  PdfReader reader = new PdfReader(sourcePdfPath);
                                  Document document = new Document(reader.GetPageSizeWithRotation(1));
                                  PdfCopy pdfCopy = new PdfCopy(document, new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
                                  MemoryStream memoryStream = new MemoryStream();
                                  PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
                                  document.AddDocListener(writer);
                                  document.Open();
                  
                                  for (int p = 1; p <= reader.NumberOfPages; p++)
                                  {
                                      if (pagesToExtract.FindIndex(s => s == p) == -1) continue;
                                      document.SetPageSize(reader.GetPageSize(p));
                                      document.NewPage();
                                      PdfContentByte cb = writer.DirectContent;
                                      PdfImportedPage pageImport = writer.GetImportedPage(reader, p);
                  
                                      int rot = reader.GetPageRotation(p);
                                      if (rot == 90 || rot == 270)
                                      {
                                          cb.AddTemplate(pageImport, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(p).Height);
                                      }
                                      else
                                      {
                                          cb.AddTemplate(pageImport, 1.0F, 0, 0, 1.0F, 0, 0);
                                      }
                  
                                      pdfCopy.AddPage(pageImport);
                                  }
                  
                                  pdfCopy.Close();
                  

                  推荐答案

                  此代码有效.您需要使用不同的文件来输出结果.

                  This code works. You need to have a different file to output the results.

                  private static void AppendToDocument(string sourcePdfPath1, string sourcePdfPath2, string outputPdfPath)
                  {
                      using (var sourceDocumentStream1 = new FileStream(sourcePdfPath1, FileMode.Open))
                      {
                          using (var sourceDocumentStream2 = new FileStream(sourcePdfPath2, FileMode.Open))
                          {
                              using (var destinationDocumentStream = new FileStream(outputPdfPath, FileMode.Create))
                              {
                                  var pdfConcat = new PdfConcatenate(destinationDocumentStream);
                                  var pdfReader = new PdfReader(sourceDocumentStream1);
                  
                                  var pages = new List<int>();
                                  for (int i = 0; i < pdfReader.NumberOfPages; i++)
                                  {
                                      pages.Add(i);
                                  }
                  
                                  pdfReader.SelectPages(pages);
                                  pdfConcat.AddPages(pdfReader);
                  
                                  pdfReader = new PdfReader(sourceDocumentStream2);
                  
                                  pages = new List<int>();
                                  for (int i = 0; i < pdfReader.NumberOfPages; i++)
                                  {
                                      pages.Add(i);
                                  }
                  
                                  pdfReader.SelectPages(pages);
                                  pdfConcat.AddPages(pdfReader);
                  
                                  pdfReader.Close();
                                  pdfConcat.Close();
                              }
                          }
                      }
                  }
                  

                  这篇关于使用 itextsharp 将页面插入现有 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PostgreSQL 日期 C# DateTime 下一篇:MySQL 数据库的正确日期时间格式是什么?

                  相关文章

                  最新文章

                    <bdo id='gDqUT'></bdo><ul id='gDqUT'></ul>

                1. <legend id='gDqUT'><style id='gDqUT'><dir id='gDqUT'><q id='gDqUT'></q></dir></style></legend>

                    1. <small id='gDqUT'></small><noframes id='gDqUT'>

                      <tfoot id='gDqUT'></tfoot>
                      <i id='gDqUT'><tr id='gDqUT'><dt id='gDqUT'><q id='gDqUT'><span id='gDqUT'><b id='gDqUT'><form id='gDqUT'><ins id='gDqUT'></ins><ul id='gDqUT'></ul><sub id='gDqUT'></sub></form><legend id='gDqUT'></legend><bdo id='gDqUT'><pre id='gDqUT'><center id='gDqUT'></center></pre></bdo></b><th id='gDqUT'></th></span></q></dt></tr></i><div id='gDqUT'><tfoot id='gDqUT'></tfoot><dl id='gDqUT'><fieldset id='gDqUT'></fieldset></dl></div>