如何在同一 pdf 文档中创建相同的 pdf 副本
how to create same copy of pdf in same pdf document
在我的程序中,用户将输入作为份数传递。
因此,如果用户传递的副本数为 2,那么我想生成单个 pdf 文档,其中相同的页面重复两次
我正在使用 PdfSharp 库
我有以下代码可以创建多页 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\");
我确定我们需要添加循环,但我还需要做些什么才能根据提供的输入在同一文档中重复相同的副本
更新
假设文档生成了 2 页,用户说副本数为 2,那么单个 pdf 文档现在将有 4 页,其中 2 页在同一文档中重复两次:
页面内容 1 页面内容 2,页面内容 1,页面内容 2
更新 2
我尝试了@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]);
}
}
但现在我收到错误消息 必须使用 pdfdocumentopenmode.import 打开 pdf 文档才能从中导入页面
好吧,这个问题和我最初理解的大相径庭。由于我对 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:代码不是 运行 针对编译器的。保留语法和关闭一个错误
如果我没理解错的话,我想你可以保存你原来的完整 PDF,然后多次打开并合并它。
如果是这样,这可能很接近。我已将您的代码与此处找到的 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);
我怀疑这也可以在不保存并重新打开文档的情况下完成,但我自己还没有使用过 pdfsharp。
最后我设法解决了自己的问题,但由于来自@Christopher 和@Blaise 的线索,我偏离了方向。这是我的最终代码
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);
在我的程序中,用户将输入作为份数传递。
因此,如果用户传递的副本数为 2,那么我想生成单个 pdf 文档,其中相同的页面重复两次
我正在使用 PdfSharp 库
我有以下代码可以创建多页 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\");
我确定我们需要添加循环,但我还需要做些什么才能根据提供的输入在同一文档中重复相同的副本
更新
假设文档生成了 2 页,用户说副本数为 2,那么单个 pdf 文档现在将有 4 页,其中 2 页在同一文档中重复两次:
页面内容 1 页面内容 2,页面内容 1,页面内容 2
更新 2
我尝试了@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]);
}
}
但现在我收到错误消息 必须使用 pdfdocumentopenmode.import 打开 pdf 文档才能从中导入页面
好吧,这个问题和我最初理解的大相径庭。由于我对 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:代码不是 运行 针对编译器的。保留语法和关闭一个错误
如果我没理解错的话,我想你可以保存你原来的完整 PDF,然后多次打开并合并它。
如果是这样,这可能很接近。我已将您的代码与此处找到的 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);
我怀疑这也可以在不保存并重新打开文档的情况下完成,但我自己还没有使用过 pdfsharp。
最后我设法解决了自己的问题,但由于来自@Christopher 和@Blaise 的线索,我偏离了方向。这是我的最终代码
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);