ABCPDF 8 - Как создать многоуровневый PDF
Мне нужно использовать ABCPDF для создания многоуровневого файла PDF. Я видел примеры для водяных знаков, но мне нужно, чтобы PDF был вторым слоем. Мой код ниже. Когда я запускаю его, я вижу только один слой. Что я делаю неправильно?
Спасибо.
WebSupergoo.ABCpdf8.Doc artworkDoc = new WebSupergoo.ABCpdf8.Doc();
artworkDoc.SetInfo(0, "License", _License);
WebSupergoo.ABCpdf8.Doc cutDoc = new WebSupergoo.ABCpdf8.Doc();
cutDoc.SetInfo(0, "License", _License);
// Attempt to read in Artwork File
try
{
artworkDoc.Read(ArtworkPath);
}
catch (Exception ex)
{
Exception noartwork = new Exception("Problem with Artwork File: " + ex.ToString());
throw noartwork;
}
// Attempt to read in cut File
try
{
cutDoc.Read(cutPath);
}
catch (Exception ex)
{
Exception nocut = new Exception("Problem with cut File: " + ex.ToString());
throw nocut;
}
WebSupergoo.ABCpdf8.Doc outputDoc = artworkDoc;
outputDoc.SetInfo(0, "License", _License);
// Attempt to merge artwork and cut files into output Document
try
{
outputDoc.PageNumber = 1;
outputDoc.Layer = outputDoc.LayerCount + 1;
outputDoc.AddImageDoc(cutDoc, 1, outputDoc.Rect);
}
catch (Exception ex)
{
Exception problem = new Exception("Problem appending cut and artwork files to output: " + ex.ToString());
throw problem;
}
// Attempt to save the output Document to the specified output path
try
{
outputDoc.Save(OutputPath);
artworkDoc.Clear();
cutDoc.Clear();
outputDoc.Clear();
}
catch (Exception ex)
{
Exception invalidOutput = new Exception("Unable to write output file: " + ex.ToString());
throw invalidOutput;
}
return true;
}
1 ответ
После использования iTextSharp для некоторых других проблем со слоями PDF, я решил изучить, как использовать iTextSharp для создания нового многослойного PDF-файла, где слои представляют собой страницы из существующих PDF-файлов. Код ниже - то, что я придумал. Код предполагает, что каждый из исходных файлов PDF имеет только одну страницу для добавления в качестве слоя в новый PDF. Мне потребовалось некоторое время, чтобы понять, так что надеюсь, что размещение здесь поможет сэкономить время другим.
try
{
iTextSharp.text.pdf.PdfReader artwork = new iTextSharp.text.pdf.PdfReader(@”c:\artwork.pdf”);
iTextSharp.text.pdf.PdfReader cut = new iTextSharp.text.pdf.PdfReader(@”c:\cut.pdf”);
//get size of the cut PDF so the new layered PDF will be sized the same.
iTextSharp.text.Rectangle pageSize = new iTextSharp.text.Rectangle(cut.GetPageSize(1).Width, cut.GetPageSize(1).Height);
//the artwork PDF needs to be sized the same as the cut pdf
if (artwork.GetPageSize(1).Width != pageSize.Width || artwork.GetPageSize(1).Height != pageSize.Height)
{
string resizedFile = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(textBox1.Text),System.IO.Path.GetFileNameWithoutExtension(textBox1.Text) + "_resized.pdf");
iTextSharp.text.Document resizedArtwork = new iTextSharp.text.Document(pageSize);
iTextSharp.text.pdf.PdfWriter resizedWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(resizedArtwork, new System.IO.FileStream(resizedFile, System.IO.FileMode.Create));
iTextSharp.text.pdf.PdfImportedPage importedPage = resizedWriter.GetImportedPage(artwork, 1);
resizedArtwork.Open();
iTextSharp.text.pdf.PdfContentByte resizedContentByte = resizedWriter.DirectContent;
resizedContentByte.AddTemplate(importedPage, 0, 0);
resizedArtwork.Close();
//close reader associated with non-resized document
artwork.Close();
//set reader to resized document
artwork = new iTextSharp.text.pdf.PdfReader(resizedFile);
}
//create a new PDF
string outputFileName = @”c:\layered.pdf”;
iTextSharp.text.Document newPDF = new iTextSharp.text.Document(pageSize);
//create writer to output new PDF's contents to
iTextSharp.text.pdf.PdfWriter pdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(newPDF, new System.IO.FileStream(outputFileName,System.IO.FileMode.Create));
//grab the first page from the artwork and cut PDF. These will become new layers in the new PDF
iTextSharp.text.pdf.PdfImportedPage artworkImportedPage = pdfWriter.GetImportedPage(artwork, 1);
iTextSharp.text.pdf.PdfImportedPage cutImportedPage = pdfWriter.GetImportedPage(cut, 1);
newPDF.Open();
iTextSharp.text.pdf.PdfContentByte pdfContentByte = pdfWriter.DirectContent;
pdfContentByte.BeginLayer(new iTextSharp.text.pdf.PdfLayer("artwork",pdfWriter));
pdfContentByte.AddTemplate(artworkImportedPage, 0, 0);
pdfContentByte.EndLayer();
pdfContentByte.BeginLayer(new iTextSharp.text.pdf.PdfLayer("cut", pdfWriter));
pdfContentByte.AddTemplate(cutImportedPage, 0, 0);
pdfContentByte.EndLayer();
newPDF.Close();
pdfWriter.Close();
artwork.Close();
cut.Close();
}
catch (Exception ex)
{
}