简体   繁体   English

如何在同一个 pdf 文档中创建 pdf 的相同副本

[英]how to create same copy of pdf in same pdf document

IN my program user passes the input as number of copies.在我的程序中,用户将输入作为副本数传递。

So if user passes number of copies as 2 then i want to generate single pdf document with same pages repeating twice因此,如果用户将副本数传递为 2,那么我想生成单个 pdf 文档,相同的页面重复两次

I am using PdfSharp library我正在使用 PdfSharp 库

I have below code which creates pdf document with multiple pages我有下面的代码,它创建了多页的 pdf 文档

var document = new PdfDocument();
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
page.Width = XUnit.FromMillimeter(111);
page.Height = XUnit.FromMillimeter(222);
//some logic to create multiple pages
document.Save("C:\mydoc\");

I am sure we need to add loop but what more i need to do to have same copies repeating in same document as per input provided我确定我们需要添加循环,但我还需要做些什么才能根据提供的输入在同一个文档中重复相同的副本

Update更新

Lets say document generated 2 pages and user says number of copies 2 then that single pdf document will now have 4 page with 2 pages repeating twice in same document:假设文档生成了 2 页,用户说副本数为 2,那么单个 pdf 文档现在将有 4 页,其中 2 页在同一文档中重复两次:

pagecontent1 pagecontent 2, pagecontent1,pagecontent2页面内容 1 页面内容 2、页面内容 1、页面内容 2

Update 2更新 2

i tried @Christophers solution and changed code like below before i save document在保存文档之前,我尝试了@Christophers 解决方案并更改了如下代码

PdfDocument outputDocument = new PdfDocument();
for (int i = 0; i < PageCopies; i++)
        {
            //the loop doing the copies
            for (int k = 0; k < document.Pages.Count; k++)
            {
                outputDocument.Pages.Add(document.Pages[k]);

            }
        }

But now i get error a pdf document must be opened with pdfdocumentopenmode.import to import pages from it但是现在我收到错误,必须使用 pdfdocumentopenmode.import 打开 pdf 文档才能从中导入页面

If I understand correctly, I think you could save your original Complete PDF, then open and merge it multiple times.如果我理解正确,我想你可以保存你原来的 Complete PDF,然后多次打开并合并它。

If that is so, this may be close.如果是这样,这可能很接近。 I've combined your code with some documentation from pdfsharp found here: http://www.pdfsharp.net/wiki/ConcatenateDocuments-sample.ashx我已将您的代码与此处找到的 pdfsharp 的一些文档相结合: http://www.pdfsharp.net/wiki/ConcatenateDocuments-sample.ashx


var document = new PdfDocument();
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
page.Width = XUnit.FromMillimeter(111);
page.Height = XUnit.FromMillimeter(222);
//some logic to create multiple pages
document.Save("OriginalDoc.pdf");

//Logic here to open and merge the document multiple times
PdfDocument outputDocument = new PdfDocument();

// Open the document to import pages from it.
PdfDocument inputDocument = PdfReader.Open("C:\mydoc\OriginalDoc.pdf", PdfDocumentOpenMode.Import);

  for(int i = 0; i < Copies; i++)
  {
    // Iterate pages
    int count = inputDocument.PageCount;
    for (int idx = 0; idx < count; idx++)
    {
      // Get the page from the external document...
      PdfPage page = inputDocument.Pages[idx];
      // ...and add it to the output document.
      outputDocument.AddPage(page);
    }
  }
  // Save the document...
  const string filename = "MergedDoc.pdf";
  outputDocument.Save(filename);

I suspect this could be done without saving and re-opening the document as well, but I haven't used pdfsharp myself.我怀疑这可以在不保存并重新打开文档的情况下完成,但我自己没有使用 pdfsharp。

Okay, the propblem turned out to be very different from what I first understood.好的,事实证明这个问题与我最初理解的非常不同。 As I have no expierience with PDFSharp in particular, I can only help with the general loop structure.由于我对 PDFSharp 没有特别的经验,我只能帮助解决一般的循环结构。

//I use a array to simulate the input PDF
int[] input = new int[]{ 1, 2 };
//And a list to simulate the output PDF
List<int> output = new List<int>();

//PageCopies is set somewhere outside this code.
//The loop counting the copies
for(int i = 0; i < PageCopies; i++){
  //the loop doing the copies
  for(int k = 0; k < input.Count; k++){
    output.Add(input[k]);
  }
}

Dislcaimer: Code not run against a compiler. Dislcaimer:代码不针对编译器运行。 Syntax and off by one errors reserved保留一个错误的语法和关闭

Finally i managed to resolve myself but offcourse due to clues from @Christopher and @Blaise.最后,由于@Christopher 和@Blaise 的线索,我设法解决了自己的问题。 Here is my final code这是我的最终代码

var document = new PdfDocument();
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
page.Width = XUnit.FromMillimeter(111);
page.Height = XUnit.FromMillimeter(222);
//some logic to create multiple pages
 document.Save("C:\mydoc\");

PdfDocument finalOutputDocument;
        if (CopyCount >1)
        {
            MemoryStream stream = new MemoryStream();
            document.Save(stream, false);
            PdfDocument source = PdfReader.Open(stream, PdfDocumentOpenMode.Import);

            finalOutputDocument= new PdfDocument();

            for (int i = 0; i < CopyCount; i++)
            {
                for (int k = 0; k < document.Pages.Count; k++)
                {
                    finalOutputDocument.Pages.Add(source.Pages[k]);
                }
            }
        }
        else
        {
            finalOutputDocument= document;
        }

        finalOutputDocument.Save(path);

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

相关问题 如何使用ghostscript同时创建2个pdf文件? - how to create 2 pdf files at the same time with ghostscript? 多个用户同时复制和复制具有相同名称的pdf文件 - Copy and pest pdf file in same name by many users in same time 在 Silverlight 中创建 PDF 文档 - create a PDF document in Silverlight 同一pdf中的风景和肖像 - Landscape and Portrait in same pdf 将页面从pdf文件复制到新文档 - Copy page from pdf file to new document 如何在打开文档时以编程方式同时打开任何应用程序(如word,pdf阅读器)的打印对话框 - How to open print dialog box of any application (like word,pdf reader) on the same time while opening the document, Programmatically 如何使用Xfinium.PDF在法定尺寸的寻呼机上创建文档? - How to create a document on the legal size pager using Xfinium.PDF? 如何创建执行搜索功能并将其导出到PDF文档的任务计划程序? - How to create a Task Scheduler executing a search function and exporting it to a PDF document? 如何创建文档模板,然后使用C#WPF转换为PDF? - How to create an Document template and then Converting to PDF using C# WPF? 如何使用XML和iTextSharp在PDF文档中创建表格? - How to create tables in a PDF document using XML and iTextSharp?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM