[英]Why my code is rotating the documents 180 degrees?
I have written a C# code that merges two pdf files upon downloading together but it rotates some (not all) documents 180 degrees.我写了一个 C# 代码,它在下载时合并两个 pdf 文件,但它会将一些(不是全部)文档旋转 180 度。 I have attached the sample file that I am uploading.
我附上了我正在上传的示例文件。
Kindly, help me on this.请帮我解决这个问题。
try
{
string NewFileName = "InspectionReport" + DateTime.Now.ToString("ddMMMyyyy HHmmss").Replace(" ", "");
destinationFile = HostingEnvironment.MapPath(@"/Downloads/CNGCertificates/" + NewFileName + ".pdf");
int f = 0;
PdfReader reader = new PdfReader(sourceFiles[f]);
int n = reader.NumberOfPages;
Document document = new Document(reader.GetPageSizeWithRotation(1));
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create));
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;
int rotation;
while (f < sourceFiles.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);
}
}
f++;
if (f < sourceFiles.Length)
{
reader = new PdfReader(sourceFiles[f]);
n = reader.NumberOfPages;
}
}
document.Close();
}
You are handling rotation
of 90
and 270
identically.您正在处理
90
度和270
度的rotation
。 Thus, if source pages with a rotation
value of 90
are handled as you expect, source pages with a rotation
value of 270
consequentially will end up upside down.因此,如果按预期处理
rotation
值为90
的源页面,则rotation
值为270
的源页面将最终颠倒。
Thus, you have to handle 270
separately, using the affine transformation rotating the other way:因此,您必须单独处理
270
,使用反方向旋转的仿射变换:
cb.AddTemplate(page, 0, 1f, -1f, 0, reader.GetPageSizeWithRotation(i).Width, 0);
Similarly you'll have to handle the 180
value separately, too, unless you want such pages to end up upside down:同样,您也必须单独处理
180
值,除非您希望此类页面最终颠倒:
cb.AddTemplate(page, -1f, 0, 0, -1f, reader.GetPageSizeWithRotation(i).Width, reader.GetPageSizeWithRotation(i).Height);
That all being said, you actually shouldn't use a normal PdfWriter
for merging at all, you should use PdfCopy
.话虽如此,您实际上根本不应该使用普通的
PdfWriter
进行合并,您应该使用PdfCopy
。 Using PdfCopy
you don't have to deal with details like rotation anymore.使用
PdfCopy
,您不必再处理旋转等细节。 Furthermore, annotations are copied along and not dropped as with PdfWriter
.此外,注释会被复制而不是像
PdfWriter
那样被删除。 Look here for example.例如看这里。 (If you use a 5.5.x version, you don't have to differentiate between the
PdfCopy
variants mentioned there anymore, you can use the plain PdfCopy
for all use cases. If you use a 4.x version, though, you do have to pick appropriately.) (如果您使用 5.5.x 版本,则不必再区分那里提到的
PdfCopy
变体,您可以对所有用例使用普通PdfCopy
。但是,如果您使用 4.x 版本,您确实有to pick appropriately.)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.