有一个 PDF 文件,您需要编写一个代码来打印这个文件,并在一张纸上拆分几页(例如,4.8 x 1),我使用库 Spire.PDF、Spire.Document(来源:https:// www.e-iceblue.com/Introduce/pdf-for-net-introduce.html#.W5d0JvYnZPY)和 ZetPdf(来源:https ://zetpdf.com/ )。如果您通过 Spire.Pdf 组件工作,那么我使用这种方法(代码取自文档)
internal static void splitPdfFileByBookmarks()
{
using (PdfDocument pdf = new PdfDocument(PdfConformanceLevel.Pdf_A1A))
{
pdf.LoadFromFile(@"d:\PdfFiles\10_11_1808_p.pdf");
pdf.FileInfo.Version = PdfVersion.Version1_7;
PdfPageBase pageBase = pdf.Pages.Add(PdfPageSize.A4, new PdfMargins());
这里我们一页放4页。我保存文档,也保存不拆分
pdf.PrintSettings.SelectMultiPageLayout(1,4);
pdf.SaveToFile(@"d:\PdfFiles\file.pdf",FileFormat.PDF);
}
}
如果您使用 ZetPdf 组件,则只有 2 页的拆分。1,但没有更多。
string filename = "Portable Document Format.pdf";
File.Copy(Path.Combine("../../../../../PDFs/", filename),
Path.Combine(Directory.GetCurrentDirectory(), filename), true);
// Create the output document
PdfDocument outputDocument = new PdfDocument();
// Show single pages
// (Note: one page contains two pages from the source document)
outputDocument.PageLayout = PdfPageLayout.SinglePage;
XFont font = new XFont("Verdana", 8, XFontStyle.Bold);
XStringFormat format = new XStringFormat();
format.Alignment = XStringAlignment.Center;
format.LineAlignment = XLineAlignment.Far;
XGraphics gfx;
XRect box;
// Open the external document as XPdfForm object
XPdfForm form = XPdfForm.FromFile(filename);
for (int idx = 0; idx < form.PageCount; idx += 2)
{
// Add a new page to the output document
在这部分代码中添加了一个页面,我尝试创建 page_2 并添加页面错误“无法将页面添加到此文档,因为该文档已拥有此页面。”
PdfPage page = outputDocument.AddPage();
page.Orientation = PageOrientation.Landscape;
double width = page.Width;
double height = page.Height;
int rotate = page.Elements.GetInteger("/Rotate");
gfx = XGraphics.FromPdfPage(page);
// Set page number (which is one-based)
form.PageNumber = idx + 1;
box = new XRect(0, 0, width/4, height/2);
// Draw the page identified by the page number like an image
gfx.DrawImage(form, box);
// Write document file name and page number on each page
box.Inflate(0, -10);
gfx.DrawString(String.Format("- {1} -", filename, idx + 1),
font, XBrushes.Red, box, format);
if (idx + 1 < form.PageCount)
{
// Set page number (which is one-based)
form.PageNumber = idx + 2;
box = new XRect(width / 4, 0, width/4 , height/2);
// Draw the page identified by the page number like an image
gfx.DrawImage(form, box);
// Write document file name and page number on each page
box.Inflate(0, -10);
gfx.DrawString(String.Format("- {1} -", filename, idx + 2),
font, XBrushes.Red, box, format);
}
}
// Save the document...
filename = "TwoPagesOnOne_tempfile.pdf";
outputDocument.Save(filename);
// ...and start a viewer.
Process.Start(filename);
有没有人遇到过这样的问题?现在我在挖掘其他组件,也许有人会告诉你一些解决方案。
他自己找到了答案,错误地处理了任务,最初我们有一个文档,我们在一张纸上形成 2 页(代码取自这里:http ://www.pdfsharp.net/wiki/TwoPagesOnOne-sample.ashx ) ,当文档形成时,我们再次对 2x1 文档重复相同的操作,结果我们在工作表上有 4 或 8 页