示例代碼:iTextSharp爲什麼重新調整文檔大小而不是合併文檔?
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace MergePDFs
{
class Program
{
static void Main(string[] args)
{
try
{
int f = 1;
// we create a reader for a certain document
PdfReader reader = new PdfReader(args[f]);
// we retrieve the total number of pagse
int n = reader.NumberOfPages;
Console.WriteLine("There are " + n + " pages in the original file.");
// step 1: creation of a document-object
Document document = new Document(reader.GetPageSizeWithRotation(1));
Console.WriteLine("PS: " + reader.GetPageSizeWithRotation(1));
// step 2: we create a writer that listens to the document
String destinationFile = args[0];//The first argument is the destination
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create));
// step 3: we open the document
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;
int rotation;
// step 4: we add content
while (f < args.Length)
{
int i = 0;
while (i < n)
{
i++;
//document.SetPageSize(reader.GetPageSizeWithRotation(i));
document.NewPage();
page = writer.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
}
else
{
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
Console.WriteLine("Processed page " + i);
}
f++;
if (f < args.Length)
{
reader = new PdfReader(args[f]);
// we retrieve the total number of pages
n = reader.NumberOfPages;
Console.WriteLine("There are " + n + " pages in the original file.");
}
}
// step 5: we close the document
document.Close();
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message);
Console.Error.WriteLine(e.StackTrace);
}
}
}
}
我希望它可以輸出相同的尺寸,這兩個文件來(這在我的情況是相同的尺寸)的文件。不幸的是,這並沒有發生,並且在合併文檔周圍正在打印白色邊框。任何幫助將不勝感激。