将页面从一个PDF文件导入到另一个PDF文件时出现问题。源PDF中的某些颜色变为红色(请参见下图)。
输入:
输出:
这是源文件:link
这是我使用的PDF合并代码:
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using Matrix = System.Drawing.Drawing2D.Matrix;
using Rectangle = iTextSharp.text.Rectangle;
namespace MergePdf
{
class Program
{
static void Main(string[] args)
{
MergeFilesToPdf(@"C:\input.pdf", @"C:\output.pdf");
}
private static void MergeFilesToPdf(string sourseFilePath, string targetFilePath)
{
using (var fs = new FileStream(targetFilePath, FileMode.Create))
{
using (var document = new Document())
{
using (var writer = PdfWriter.GetInstance(document, fs))
{
document.Open();
ImportFile(document, writer, sourseFilePath);
document.Close();
}
}
}
}
private static void ImportFile(Document document, PdfWriter writer, string sourseFilePath)
{
using (var reader = new PdfReader(sourseFilePath))
{
for (var i = 1; i <= reader.NumberOfPages; i++)
{
var documentPageSizeWidthPt = Utilities.MillimetersToPoints(200);
var documentPageSizeHeightPt = Utilities.MillimetersToPoints(200);
document.SetPageSize(new Rectangle(documentPageSizeWidthPt, documentPageSizeHeightPt));
document.NewPage();
Rectangle imageSizePt = reader.GetPageSize(i);
SizeF resizedSizePt = new SizeF(500, 300);
PdfImportedPage imp = writer.GetImportedPage(reader, i);
var tm = new Matrix();
var x = (documentPageSizeWidthPt - resizedSizePt.Width) / 2;
var y = (documentPageSizeHeightPt - resizedSizePt.Height) / 2;
tm.Scale(resizedSizePt.Width / imageSizePt.Width, resizedSizePt.Height / imageSizePt.Height, MatrixOrder.Append);
tm.Translate(x, y, MatrixOrder.Append);
writer.DirectContent.AddTemplate(imp, tm.Elements[0], tm.Elements[1], tm.Elements[2], tm.Elements[3], tm.Elements[4], tm.Elements[5]);
}
writer.FreeReader(reader);
}
}
}
}
我尝试使用iText7,但问题并没有消失。你能帮忙吗?