gpt4 book ai didi

android - PDF 内容向右移动并为更宽的页面剪切

转载 作者:行者123 更新时间:2023-11-30 02:37:26 27 4
gpt4 key购买 nike

ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bmp = Bitmap.createScaledBitmap(pdfBtm, (int) (pdfBtm.getWidth()), (int) (pdfBtm.getHeight()), true);

bmp.compress(CompressFormat.PNG, 0, stream);

byte[] byteArray = stream.toByteArray();

Image img = Image.getInstance(byteArray);

String pdffile = sharedPref.getString(com.appealsoft.i_file_me.Config.PdffileName, "");
int pageNumber = sharedPref.getInt(com.appealsoft.i_file_me.Config.PdfpageNumber, 0);

PdfReader reader = new PdfReader(pdffile);

String filename = pdffile.substring(pdffile.lastIndexOf("/") + 1, pdffile.length());
System.out.println("file name is :" + filename);

OutputStream newfile = new FileOutputStream(new File("/sdcard/" + filename));
Document newDocs = new Document();
PdfWriter writer = PdfWriter.getInstance(newDocs, newfile);
newDocs.open();

for (int i = 1; i <= reader.getNumberOfPages(); i++)
{
if (i == pageNumber)
{
Image img2 = Image.getInstance(byteArray);
newDocs.add(img2);

System.out.println(" i was inside...");
} else {
Image img2 = Image.getInstance(writer.getImportedPage(reader, i));
newDocs.add(img2);
}
}
newDocs.close();

当我用这种方法创建 PDF 时,PDF 的内容会移到右侧。对于更宽的 PDF 页面,页面的某些部分会被剪切。

有人知道为什么会这样吗?

最佳答案

When I create PDF with this method, contents of PDF get shifted to right side. For wider PDF pages, some of the part of page gets cut.

Anyone know why this is so?

因为这就是代码告诉 iText 要做的事情:它从一些源文档(包括它的边距)中获取一个完整的页面,并将它添加到 iText 在已经有自己的边距的新页面的正文中安排的内容.

OP 的实际目标似乎是用某些图像替换某些文档中的单个页面。为此应使用 PdfStamperPdfCopy 实例。

例如使用 PdfStamper 你可以做这样的事情:

PdfReader reader = new PdfReader(SOURCE);
int pageToReplace = NROFPAGETOREPLACE;

List<Integer> pagesToKeep = new ArrayList<Integer>();
for (int i = 1; i <= reader.getNumberOfPages(); i++)
if (i != pageToReplace) pagesToKeep.add(i);

reader.selectPages(pagesToKeep);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(TARGET));

stamper.insertPage(pageToReplace, reader.getPageSizeWithRotation(1));
Image image = Image.getInstance(IMAGESOURCE);
stamper.getOverContent(pageToReplace).addImage(image, image.getWidth(), 0, 0, image.getHeight(), 30, 30);

stamper.close();

PS:您的评论似乎表明添加到新页面的图片实际上包含了所有内容。在这种情况下,将上面示例的最后几行替换为

是合适的
Image image = Image.getInstance(IMAGESOURCE);
stamper.insertPage(pageToReplace, new Rectangle(image.getWidth(), image.getHeight());
stamper.getOverContent(pageToReplace).addImage(image, image.getWidth(), 0, 0, image.getHeight(), 0, 0);

stamper.close();

关于android - PDF 内容向右移动并为更宽的页面剪切,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26275936/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com