C# 合并文件后,图像颜色从白色变为红色

C# 合并文件后,图像颜色从白色变为红色,c#,.net,itext,C#,.net,Itext,我在将页面从一个PDF文件导入到另一个PDF文件时遇到问题。源PDF中的某些颜色变为红色(请参见下图) 输入: 输出: 这是源文件: 这是我使用的PDF合并代码: using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; using Matrix = Syst

我在将页面从一个PDF文件导入到另一个PDF文件时遇到问题。源PDF中的某些颜色变为红色(请参见下图)

输入:

输出:

这是源文件:

这是我使用的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);
            }
        }
    }
}
使用系统诊断;
使用系统图;
使用System.Drawing.Drawing2D;
使用System.IO;
使用iTextSharp.text;
使用iTextSharp.text.pdf;
使用矩阵=System.Drawing.Drawing2D.Matrix;
使用Rectangle=iTextSharp.text.Rectangle;
命名空间合并PDF
{
班级计划
{
静态void Main(字符串[]参数)
{
MergeFilesToPdf(@“C:\input.pdf”,“C:\output.pdf”);
}
私有静态void MergeFilesToPdf(字符串sourseFilePath,字符串targetFilePath)
{
使用(var fs=new FileStream(targetFilePath,FileMode.Create))
{
使用(var document=new document())
{
使用(var writer=PdfWriter.GetInstance(document,fs))
{
document.Open();
导入文件(文档、编写器、源文件路径);
document.Close();
}
}
}
}
私有静态void导入文件(文档文档、PdfWriter编写器、字符串源文件路径)
{
使用(var reader=newpdfreader(sourseFilePath))
{

对于(var i=1;i来说,原因是源文件已经包含红色图形,只是默认情况下不显示,因为红色图形位于可选内容组(Adobe Acrobat中称为“层”)中,该内容组开始不可见:

禁用红色的图层

启用红色的图层

iText页面导入不完全支持可选内容组复制,因此包括红色绘图在内的整个内容在页面副本中可见

为了确保您的副本不会显示任何意外内容,您应该从源文件中删除这些可选内容组的内容,而不仅仅是禁用这些组